blob: e55d6d2fd1594907c131d5d3bd1542e0c7392c6f [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill80a6fc02015-08-21 16:53:16 -040014#include "common/BitSetIterator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
18#include "common/version.h"
19#include "compiler/translator/blocklayout.h"
Jamie Madill9082b982016-04-27 15:21:51 -040020#include "libANGLE/ContextState.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050022#include "libANGLE/features.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040023#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050024#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill62d31cb2015-09-11 13:25:51 -040025#include "libANGLE/queryconversions.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040026#include "libANGLE/Uniform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028namespace gl
29{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000030
Geoff Lang7dd2e102014-11-10 15:19:26 -050031namespace
32{
33
Jamie Madill62d31cb2015-09-11 13:25:51 -040034void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
35{
36 stream->writeInt(var.type);
37 stream->writeInt(var.precision);
38 stream->writeString(var.name);
39 stream->writeString(var.mappedName);
40 stream->writeInt(var.arraySize);
41 stream->writeInt(var.staticUse);
42 stream->writeString(var.structName);
43 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050044}
45
Jamie Madill62d31cb2015-09-11 13:25:51 -040046void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
47{
48 var->type = stream->readInt<GLenum>();
49 var->precision = stream->readInt<GLenum>();
50 var->name = stream->readString();
51 var->mappedName = stream->readString();
52 var->arraySize = stream->readInt<unsigned int>();
53 var->staticUse = stream->readBool();
54 var->structName = stream->readString();
55}
56
Jamie Madill62d31cb2015-09-11 13:25:51 -040057// This simplified cast function doesn't need to worry about advanced concepts like
58// depth range values, or casting to bool.
59template <typename DestT, typename SrcT>
60DestT UniformStateQueryCast(SrcT value);
61
62// From-Float-To-Integer Casts
63template <>
64GLint UniformStateQueryCast(GLfloat value)
65{
66 return clampCast<GLint>(roundf(value));
67}
68
69template <>
70GLuint UniformStateQueryCast(GLfloat value)
71{
72 return clampCast<GLuint>(roundf(value));
73}
74
75// From-Integer-to-Integer Casts
76template <>
77GLint UniformStateQueryCast(GLuint value)
78{
79 return clampCast<GLint>(value);
80}
81
82template <>
83GLuint UniformStateQueryCast(GLint value)
84{
85 return clampCast<GLuint>(value);
86}
87
88// From-Boolean-to-Anything Casts
89template <>
90GLfloat UniformStateQueryCast(GLboolean value)
91{
92 return (value == GL_TRUE ? 1.0f : 0.0f);
93}
94
95template <>
96GLint UniformStateQueryCast(GLboolean value)
97{
98 return (value == GL_TRUE ? 1 : 0);
99}
100
101template <>
102GLuint UniformStateQueryCast(GLboolean value)
103{
104 return (value == GL_TRUE ? 1u : 0u);
105}
106
107// Default to static_cast
108template <typename DestT, typename SrcT>
109DestT UniformStateQueryCast(SrcT value)
110{
111 return static_cast<DestT>(value);
112}
113
114template <typename SrcT, typename DestT>
115void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
116{
117 for (int comp = 0; comp < components; ++comp)
118 {
119 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
120 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
121 size_t offset = comp * 4;
122 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
123 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
124 }
125}
126
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400127bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
128{
129 for (const LinkedUniform &uniform : list)
130 {
131 if (uniform.name == name)
132 return true;
133 }
134
135 return false;
136}
137
Jamie Madill62d31cb2015-09-11 13:25:51 -0400138} // anonymous namespace
139
Jamie Madill4a3c2342015-10-08 12:58:45 -0400140const char *const g_fakepath = "C:\\fakepath";
141
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400142InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000143{
144}
145
146InfoLog::~InfoLog()
147{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000148}
149
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400150size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000151{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400152 const std::string &logString = mStream.str();
153 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000154}
155
Geoff Lange1a27752015-10-05 13:16:04 -0400156void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000157{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400158 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000159
160 if (bufSize > 0)
161 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400162 const std::string str(mStream.str());
163
164 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000165 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400166 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
167 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000168 }
169
170 infoLog[index] = '\0';
171 }
172
173 if (length)
174 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400175 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000176 }
177}
178
179// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300180// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000181// messages, so lets remove all occurrences of this fake file path from the log.
182void InfoLog::appendSanitized(const char *message)
183{
184 std::string msg(message);
185
186 size_t found;
187 do
188 {
189 found = msg.find(g_fakepath);
190 if (found != std::string::npos)
191 {
192 msg.erase(found, strlen(g_fakepath));
193 }
194 }
195 while (found != std::string::npos);
196
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400197 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000198}
199
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000200void InfoLog::reset()
201{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000202}
203
Geoff Langd8605522016-04-13 10:19:12 -0400204VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000205{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500206}
207
Geoff Langd8605522016-04-13 10:19:12 -0400208VariableLocation::VariableLocation(const std::string &name,
209 unsigned int element,
210 unsigned int index)
211 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500212{
213}
214
Geoff Langd8605522016-04-13 10:19:12 -0400215void Program::Bindings::bindLocation(GLuint index, const std::string &name)
216{
217 mBindings[name] = index;
218}
219
220int Program::Bindings::getBinding(const std::string &name) const
221{
222 auto iter = mBindings.find(name);
223 return (iter != mBindings.end()) ? iter->second : -1;
224}
225
226Program::Bindings::const_iterator Program::Bindings::begin() const
227{
228 return mBindings.begin();
229}
230
231Program::Bindings::const_iterator Program::Bindings::end() const
232{
233 return mBindings.end();
234}
235
Jamie Madill48ef11b2016-04-27 15:21:52 -0400236ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500237 : mLabel(),
238 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400239 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300240 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500241 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
242 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400243{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300244 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400245}
246
Jamie Madill48ef11b2016-04-27 15:21:52 -0400247ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400248{
249 if (mAttachedVertexShader != nullptr)
250 {
251 mAttachedVertexShader->release();
252 }
253
254 if (mAttachedFragmentShader != nullptr)
255 {
256 mAttachedFragmentShader->release();
257 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300258
259 if (mAttachedComputeShader != nullptr)
260 {
261 mAttachedComputeShader->release();
262 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400263}
264
Jamie Madill48ef11b2016-04-27 15:21:52 -0400265const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500266{
267 return mLabel;
268}
269
Jamie Madill48ef11b2016-04-27 15:21:52 -0400270const LinkedUniform *ProgramState::getUniformByName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400271{
272 for (const LinkedUniform &linkedUniform : mUniforms)
273 {
274 if (linkedUniform.name == name)
275 {
276 return &linkedUniform;
277 }
278 }
279
280 return nullptr;
281}
282
Jamie Madill48ef11b2016-04-27 15:21:52 -0400283GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400284{
285 size_t subscript = GL_INVALID_INDEX;
286 std::string baseName = gl::ParseUniformName(name, &subscript);
287
288 for (size_t location = 0; location < mUniformLocations.size(); ++location)
289 {
290 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400291 if (!uniformLocation.used)
292 {
293 continue;
294 }
295
296 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400297
298 if (uniform.name == baseName)
299 {
Geoff Langd8605522016-04-13 10:19:12 -0400300 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400301 {
Geoff Langd8605522016-04-13 10:19:12 -0400302 if (uniformLocation.element == subscript ||
303 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
304 {
305 return static_cast<GLint>(location);
306 }
307 }
308 else
309 {
310 if (subscript == GL_INVALID_INDEX)
311 {
312 return static_cast<GLint>(location);
313 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400314 }
315 }
316 }
317
318 return -1;
319}
320
Jamie Madill48ef11b2016-04-27 15:21:52 -0400321GLuint ProgramState::getUniformIndex(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400322{
323 size_t subscript = GL_INVALID_INDEX;
324 std::string baseName = gl::ParseUniformName(name, &subscript);
325
326 // The app is not allowed to specify array indices other than 0 for arrays of basic types
327 if (subscript != 0 && subscript != GL_INVALID_INDEX)
328 {
329 return GL_INVALID_INDEX;
330 }
331
332 for (size_t index = 0; index < mUniforms.size(); index++)
333 {
334 const LinkedUniform &uniform = mUniforms[index];
335 if (uniform.name == baseName)
336 {
337 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
338 {
339 return static_cast<GLuint>(index);
340 }
341 }
342 }
343
344 return GL_INVALID_INDEX;
345}
346
Jamie Madill7aea7e02016-05-10 10:39:45 -0400347Program::Program(rx::GLImplFactory *factory, ResourceManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400348 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400349 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500350 mLinked(false),
351 mDeleteStatus(false),
352 mRefCount(0),
353 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400354 mHandle(handle),
355 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500356{
357 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000358
359 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500360 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361}
362
363Program::~Program()
364{
365 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000366
Geoff Lang7dd2e102014-11-10 15:19:26 -0500367 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368}
369
Geoff Lang70d0f492015-12-10 17:45:46 -0500370void Program::setLabel(const std::string &label)
371{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400372 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500373}
374
375const std::string &Program::getLabel() const
376{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400377 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500378}
379
Jamie Madillef300b12016-10-07 15:12:09 -0400380void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300382 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300384 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 {
Jamie Madillef300b12016-10-07 15:12:09 -0400386 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300387 mState.mAttachedVertexShader = shader;
388 mState.mAttachedVertexShader->addRef();
389 break;
390 }
391 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392 {
Jamie Madillef300b12016-10-07 15:12:09 -0400393 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300394 mState.mAttachedFragmentShader = shader;
395 mState.mAttachedFragmentShader->addRef();
396 break;
397 }
398 case GL_COMPUTE_SHADER:
399 {
Jamie Madillef300b12016-10-07 15:12:09 -0400400 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300401 mState.mAttachedComputeShader = shader;
402 mState.mAttachedComputeShader->addRef();
403 break;
404 }
405 default:
406 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408}
409
410bool Program::detachShader(Shader *shader)
411{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300412 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300414 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300416 if (mState.mAttachedVertexShader != shader)
417 {
418 return false;
419 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420
Martin Radev4c4c8e72016-08-04 12:25:34 +0300421 shader->release();
422 mState.mAttachedVertexShader = nullptr;
423 break;
424 }
425 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300427 if (mState.mAttachedFragmentShader != shader)
428 {
429 return false;
430 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431
Martin Radev4c4c8e72016-08-04 12:25:34 +0300432 shader->release();
433 mState.mAttachedFragmentShader = nullptr;
434 break;
435 }
436 case GL_COMPUTE_SHADER:
437 {
438 if (mState.mAttachedComputeShader != shader)
439 {
440 return false;
441 }
442
443 shader->release();
444 mState.mAttachedComputeShader = nullptr;
445 break;
446 }
447 default:
448 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000451 return true;
452}
453
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000454int Program::getAttachedShadersCount() const
455{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300456 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
457 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000458}
459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460void Program::bindAttributeLocation(GLuint index, const char *name)
461{
Geoff Langd8605522016-04-13 10:19:12 -0400462 mAttributeBindings.bindLocation(index, name);
463}
464
465void Program::bindUniformLocation(GLuint index, const char *name)
466{
467 // Bind the base uniform name only since array indices other than 0 cannot be bound
468 mUniformBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469}
470
Sami Väisänen46eaa942016-06-29 10:26:37 +0300471void Program::bindFragmentInputLocation(GLint index, const char *name)
472{
473 mFragmentInputBindings.bindLocation(index, name);
474}
475
476BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
477{
478 BindingInfo ret;
479 ret.type = GL_NONE;
480 ret.valid = false;
481
482 const Shader *fragmentShader = mState.getAttachedFragmentShader();
483 ASSERT(fragmentShader);
484
485 // Find the actual fragment shader varying we're interested in
486 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
487
488 for (const auto &binding : mFragmentInputBindings)
489 {
490 if (binding.second != static_cast<GLuint>(index))
491 continue;
492
493 ret.valid = true;
494
495 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400496 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300497
498 for (const auto &in : inputs)
499 {
500 if (in.name == originalName)
501 {
502 if (in.isArray())
503 {
504 // The client wants to bind either "name" or "name[0]".
505 // GL ES 3.1 spec refers to active array names with language such as:
506 // "if the string identifies the base name of an active array, where the
507 // string would exactly match the name of the variable if the suffix "[0]"
508 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400509 if (arrayIndex == GL_INVALID_INDEX)
510 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300511
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400512 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300513 }
514 else
515 {
516 ret.name = in.mappedName;
517 }
518 ret.type = in.type;
519 return ret;
520 }
521 }
522 }
523
524 return ret;
525}
526
527void Program::pathFragmentInputGen(GLint index,
528 GLenum genMode,
529 GLint components,
530 const GLfloat *coeffs)
531{
532 // If the location is -1 then the command is silently ignored
533 if (index == -1)
534 return;
535
536 const auto &binding = getFragmentInputBindingInfo(index);
537
538 // If the input doesn't exist then then the command is silently ignored
539 // This could happen through optimization for example, the shader translator
540 // decides that a variable is not actually being used and optimizes it away.
541 if (binding.name.empty())
542 return;
543
544 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
545}
546
Martin Radev4c4c8e72016-08-04 12:25:34 +0300547// The attached shaders are checked for linking errors by matching up their variables.
548// Uniform, input and output variables get collected.
549// The code gets compiled into binaries.
Jamie Madill9082b982016-04-27 15:21:51 -0400550Error Program::link(const ContextState &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000551{
552 unlink(false);
553
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000554 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000555 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000556
Martin Radev4c4c8e72016-08-04 12:25:34 +0300557 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500558
Martin Radev4c4c8e72016-08-04 12:25:34 +0300559 bool isComputeShaderAttached = (mState.mAttachedComputeShader != nullptr);
560 bool nonComputeShadersAttached =
561 (mState.mAttachedVertexShader != nullptr || mState.mAttachedFragmentShader != nullptr);
562 // Check whether we both have a compute and non-compute shaders attached.
563 // If there are of both types attached, then linking should fail.
564 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
565 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500566 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300567 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
568 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400569 }
570
Martin Radev4c4c8e72016-08-04 12:25:34 +0300571 if (mState.mAttachedComputeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500572 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300573 if (!mState.mAttachedComputeShader->isCompiled())
574 {
575 mInfoLog << "Attached compute shader is not compiled.";
576 return NoError();
577 }
578 ASSERT(mState.mAttachedComputeShader->getType() == GL_COMPUTE_SHADER);
579
580 mState.mComputeShaderLocalSize = mState.mAttachedComputeShader->getWorkGroupSize();
581
582 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
583 // If the work group size is not specified, a link time error should occur.
584 if (!mState.mComputeShaderLocalSize.isDeclared())
585 {
586 mInfoLog << "Work group size is not specified.";
587 return NoError();
588 }
589
590 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
591 {
592 return NoError();
593 }
594
595 if (!linkUniformBlocks(mInfoLog, caps))
596 {
597 return NoError();
598 }
599
Jamie Madillb0a838b2016-11-13 20:02:12 -0500600 ANGLE_TRY_RESULT(mProgram->link(data, mInfoLog), mLinked);
601 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300602 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500603 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300604 }
605 }
606 else
607 {
608 if (!mState.mAttachedFragmentShader || !mState.mAttachedFragmentShader->isCompiled())
609 {
610 return NoError();
611 }
612 ASSERT(mState.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
613
614 if (!mState.mAttachedVertexShader || !mState.mAttachedVertexShader->isCompiled())
615 {
616 return NoError();
617 }
618 ASSERT(mState.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
619
620 if (mState.mAttachedFragmentShader->getShaderVersion() !=
621 mState.mAttachedVertexShader->getShaderVersion())
622 {
623 mInfoLog << "Fragment shader version does not match vertex shader version.";
624 return NoError();
625 }
626
Jamie Madilleb979bf2016-11-15 12:28:46 -0500627 if (!linkAttributes(data, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300628 {
629 return NoError();
630 }
631
632 if (!linkVaryings(mInfoLog, mState.mAttachedVertexShader, mState.mAttachedFragmentShader))
633 {
634 return NoError();
635 }
636
637 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
638 {
639 return NoError();
640 }
641
642 if (!linkUniformBlocks(mInfoLog, caps))
643 {
644 return NoError();
645 }
646
647 const auto &mergedVaryings = getMergedVaryings();
648
649 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
650 {
651 return NoError();
652 }
653
654 linkOutputVariables();
655
Jamie Madillb0a838b2016-11-13 20:02:12 -0500656 ANGLE_TRY_RESULT(mProgram->link(data, mInfoLog), mLinked);
657 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300658 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500659 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300660 }
661
662 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500663 }
664
Jamie Madill4a3c2342015-10-08 12:58:45 -0400665 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400666
Martin Radev4c4c8e72016-08-04 12:25:34 +0300667 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000668}
669
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000670// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000671void Program::unlink(bool destroy)
672{
673 if (destroy) // Object being destructed
674 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400675 if (mState.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000676 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400677 mState.mAttachedFragmentShader->release();
678 mState.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679 }
680
Jamie Madill48ef11b2016-04-27 15:21:52 -0400681 if (mState.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400683 mState.mAttachedVertexShader->release();
684 mState.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685 }
Martin Radev4c4c8e72016-08-04 12:25:34 +0300686
687 if (mState.mAttachedComputeShader)
688 {
689 mState.mAttachedComputeShader->release();
690 mState.mAttachedComputeShader = nullptr;
691 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692 }
693
Jamie Madill48ef11b2016-04-27 15:21:52 -0400694 mState.mAttributes.clear();
695 mState.mActiveAttribLocationsMask.reset();
696 mState.mTransformFeedbackVaryingVars.clear();
697 mState.mUniforms.clear();
698 mState.mUniformLocations.clear();
699 mState.mUniformBlocks.clear();
700 mState.mOutputVariables.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300701 mState.mComputeShaderLocalSize.fill(1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500702
Geoff Lang7dd2e102014-11-10 15:19:26 -0500703 mValidated = false;
704
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000705 mLinked = false;
706}
707
Geoff Lange1a27752015-10-05 13:16:04 -0400708bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000709{
710 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000711}
712
Geoff Lang7dd2e102014-11-10 15:19:26 -0500713Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000714{
715 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000716
Geoff Lang7dd2e102014-11-10 15:19:26 -0500717#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
718 return Error(GL_NO_ERROR);
719#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400720 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
721 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000722 {
Jamie Madillf6113162015-05-07 11:49:21 -0400723 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500724 return Error(GL_NO_ERROR);
725 }
726
Geoff Langc46cc2f2015-10-01 17:16:20 -0400727 BinaryInputStream stream(binary, length);
728
Geoff Lang7dd2e102014-11-10 15:19:26 -0500729 int majorVersion = stream.readInt<int>();
730 int minorVersion = stream.readInt<int>();
731 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
732 {
Jamie Madillf6113162015-05-07 11:49:21 -0400733 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500734 return Error(GL_NO_ERROR);
735 }
736
737 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
738 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
739 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
740 {
Jamie Madillf6113162015-05-07 11:49:21 -0400741 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500742 return Error(GL_NO_ERROR);
743 }
744
Martin Radev4c4c8e72016-08-04 12:25:34 +0300745 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
746 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
747 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
748
Jamie Madill63805b42015-08-25 13:17:39 -0400749 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
750 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400751 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500752
Jamie Madill3da79b72015-04-27 11:09:17 -0400753 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400754 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400755 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
756 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400757 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400758 LoadShaderVar(&stream, &attrib);
759 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400760 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400761 }
762
Jamie Madill62d31cb2015-09-11 13:25:51 -0400763 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400764 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400765 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
766 {
767 LinkedUniform uniform;
768 LoadShaderVar(&stream, &uniform);
769
770 uniform.blockIndex = stream.readInt<int>();
771 uniform.blockInfo.offset = stream.readInt<int>();
772 uniform.blockInfo.arrayStride = stream.readInt<int>();
773 uniform.blockInfo.matrixStride = stream.readInt<int>();
774 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
775
Jamie Madill48ef11b2016-04-27 15:21:52 -0400776 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400777 }
778
779 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400780 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400781 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
782 uniformIndexIndex++)
783 {
784 VariableLocation variable;
785 stream.readString(&variable.name);
786 stream.readInt(&variable.element);
787 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400788 stream.readBool(&variable.used);
789 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400790
Jamie Madill48ef11b2016-04-27 15:21:52 -0400791 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400792 }
793
794 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400795 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400796 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
797 ++uniformBlockIndex)
798 {
799 UniformBlock uniformBlock;
800 stream.readString(&uniformBlock.name);
801 stream.readBool(&uniformBlock.isArray);
802 stream.readInt(&uniformBlock.arrayElement);
803 stream.readInt(&uniformBlock.dataSize);
804 stream.readBool(&uniformBlock.vertexStaticUse);
805 stream.readBool(&uniformBlock.fragmentStaticUse);
806
807 unsigned int numMembers = stream.readInt<unsigned int>();
808 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
809 {
810 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
811 }
812
Jamie Madill48ef11b2016-04-27 15:21:52 -0400813 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400814 }
815
Brandon Jones1048ea72015-10-06 15:34:52 -0700816 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400817 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700818 for (unsigned int transformFeedbackVaryingIndex = 0;
819 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
820 ++transformFeedbackVaryingIndex)
821 {
822 sh::Varying varying;
823 stream.readInt(&varying.arraySize);
824 stream.readInt(&varying.type);
825 stream.readString(&varying.name);
826
Jamie Madill48ef11b2016-04-27 15:21:52 -0400827 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700828 }
829
Jamie Madill48ef11b2016-04-27 15:21:52 -0400830 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400831
Jamie Madill80a6fc02015-08-21 16:53:16 -0400832 unsigned int outputVarCount = stream.readInt<unsigned int>();
833 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
834 {
835 int locationIndex = stream.readInt<int>();
836 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400837 stream.readInt(&locationData.element);
838 stream.readInt(&locationData.index);
839 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400840 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400841 }
842
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400843 stream.readInt(&mSamplerUniformRange.start);
844 stream.readInt(&mSamplerUniformRange.end);
845
Jamie Madillb0a838b2016-11-13 20:02:12 -0500846 ANGLE_TRY_RESULT(mProgram->load(mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000847
Jamie Madillb0a838b2016-11-13 20:02:12 -0500848 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500849#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
850}
851
852Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
853{
854 if (binaryFormat)
855 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400856 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500857 }
858
859 BinaryOutputStream stream;
860
Geoff Lang7dd2e102014-11-10 15:19:26 -0500861 stream.writeInt(ANGLE_MAJOR_VERSION);
862 stream.writeInt(ANGLE_MINOR_VERSION);
863 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
864
Martin Radev4c4c8e72016-08-04 12:25:34 +0300865 stream.writeInt(mState.mComputeShaderLocalSize[0]);
866 stream.writeInt(mState.mComputeShaderLocalSize[1]);
867 stream.writeInt(mState.mComputeShaderLocalSize[2]);
868
Jamie Madill48ef11b2016-04-27 15:21:52 -0400869 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500870
Jamie Madill48ef11b2016-04-27 15:21:52 -0400871 stream.writeInt(mState.mAttributes.size());
872 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400873 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400874 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400875 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400876 }
877
Jamie Madill48ef11b2016-04-27 15:21:52 -0400878 stream.writeInt(mState.mUniforms.size());
879 for (const gl::LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400880 {
881 WriteShaderVar(&stream, uniform);
882
883 // FIXME: referenced
884
885 stream.writeInt(uniform.blockIndex);
886 stream.writeInt(uniform.blockInfo.offset);
887 stream.writeInt(uniform.blockInfo.arrayStride);
888 stream.writeInt(uniform.blockInfo.matrixStride);
889 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
890 }
891
Jamie Madill48ef11b2016-04-27 15:21:52 -0400892 stream.writeInt(mState.mUniformLocations.size());
893 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400894 {
895 stream.writeString(variable.name);
896 stream.writeInt(variable.element);
897 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400898 stream.writeInt(variable.used);
899 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400900 }
901
Jamie Madill48ef11b2016-04-27 15:21:52 -0400902 stream.writeInt(mState.mUniformBlocks.size());
903 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400904 {
905 stream.writeString(uniformBlock.name);
906 stream.writeInt(uniformBlock.isArray);
907 stream.writeInt(uniformBlock.arrayElement);
908 stream.writeInt(uniformBlock.dataSize);
909
910 stream.writeInt(uniformBlock.vertexStaticUse);
911 stream.writeInt(uniformBlock.fragmentStaticUse);
912
913 stream.writeInt(uniformBlock.memberUniformIndexes.size());
914 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
915 {
916 stream.writeInt(memberUniformIndex);
917 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400918 }
919
Jamie Madill48ef11b2016-04-27 15:21:52 -0400920 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
921 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -0700922 {
923 stream.writeInt(varying.arraySize);
924 stream.writeInt(varying.type);
925 stream.writeString(varying.name);
926 }
927
Jamie Madill48ef11b2016-04-27 15:21:52 -0400928 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400929
Jamie Madill48ef11b2016-04-27 15:21:52 -0400930 stream.writeInt(mState.mOutputVariables.size());
931 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -0400932 {
933 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -0400934 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400935 stream.writeInt(outputPair.second.index);
936 stream.writeString(outputPair.second.name);
937 }
938
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400939 stream.writeInt(mSamplerUniformRange.start);
940 stream.writeInt(mSamplerUniformRange.end);
941
Geoff Lang7dd2e102014-11-10 15:19:26 -0500942 gl::Error error = mProgram->save(&stream);
943 if (error.isError())
944 {
945 return error;
946 }
947
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700948 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -0400949 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500950
951 if (streamLength > bufSize)
952 {
953 if (length)
954 {
955 *length = 0;
956 }
957
958 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
959 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
960 // sizes and then copy it.
961 return Error(GL_INVALID_OPERATION);
962 }
963
964 if (binary)
965 {
966 char *ptr = reinterpret_cast<char*>(binary);
967
Jamie Madill48ef11b2016-04-27 15:21:52 -0400968 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500969 ptr += streamLength;
970
971 ASSERT(ptr - streamLength == binary);
972 }
973
974 if (length)
975 {
976 *length = streamLength;
977 }
978
979 return Error(GL_NO_ERROR);
980}
981
982GLint Program::getBinaryLength() const
983{
984 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400985 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500986 if (error.isError())
987 {
988 return 0;
989 }
990
991 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000992}
993
Geoff Langc5629752015-12-07 16:29:04 -0500994void Program::setBinaryRetrievableHint(bool retrievable)
995{
996 // TODO(jmadill) : replace with dirty bits
997 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400998 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500999}
1000
1001bool Program::getBinaryRetrievableHint() const
1002{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001003 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001004}
1005
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001006void Program::release()
1007{
1008 mRefCount--;
1009
1010 if (mRefCount == 0 && mDeleteStatus)
1011 {
1012 mResourceManager->deleteProgram(mHandle);
1013 }
1014}
1015
1016void Program::addRef()
1017{
1018 mRefCount++;
1019}
1020
1021unsigned int Program::getRefCount() const
1022{
1023 return mRefCount;
1024}
1025
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001026int Program::getInfoLogLength() const
1027{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001028 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001029}
1030
Geoff Lange1a27752015-10-05 13:16:04 -04001031void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001032{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001033 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001034}
1035
Geoff Lange1a27752015-10-05 13:16:04 -04001036void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001037{
1038 int total = 0;
1039
Martin Radev4c4c8e72016-08-04 12:25:34 +03001040 if (mState.mAttachedComputeShader)
1041 {
1042 if (total < maxCount)
1043 {
1044 shaders[total] = mState.mAttachedComputeShader->getHandle();
1045 total++;
1046 }
1047 }
1048
Jamie Madill48ef11b2016-04-27 15:21:52 -04001049 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001050 {
1051 if (total < maxCount)
1052 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001053 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001054 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001055 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001056 }
1057
Jamie Madill48ef11b2016-04-27 15:21:52 -04001058 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001059 {
1060 if (total < maxCount)
1061 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001062 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001063 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001064 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001065 }
1066
1067 if (count)
1068 {
1069 *count = total;
1070 }
1071}
1072
Geoff Lange1a27752015-10-05 13:16:04 -04001073GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001074{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001075 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001077 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001078 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001079 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001080 }
1081 }
1082
Austin Kinrossb8af7232015-03-16 22:33:25 -07001083 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001084}
1085
Jamie Madill63805b42015-08-25 13:17:39 -04001086bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001087{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001088 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1089 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090}
1091
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001092void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1093{
Jamie Madillc349ec02015-08-21 16:53:12 -04001094 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001095 {
1096 if (bufsize > 0)
1097 {
1098 name[0] = '\0';
1099 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001100
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001101 if (length)
1102 {
1103 *length = 0;
1104 }
1105
1106 *type = GL_NONE;
1107 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001108 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001109 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001110
1111 size_t attributeIndex = 0;
1112
Jamie Madill48ef11b2016-04-27 15:21:52 -04001113 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001114 {
1115 // Skip over inactive attributes
1116 if (attribute.staticUse)
1117 {
1118 if (static_cast<size_t>(index) == attributeIndex)
1119 {
1120 break;
1121 }
1122 attributeIndex++;
1123 }
1124 }
1125
Jamie Madill48ef11b2016-04-27 15:21:52 -04001126 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1127 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001128
1129 if (bufsize > 0)
1130 {
1131 const char *string = attrib.name.c_str();
1132
1133 strncpy(name, string, bufsize);
1134 name[bufsize - 1] = '\0';
1135
1136 if (length)
1137 {
1138 *length = static_cast<GLsizei>(strlen(name));
1139 }
1140 }
1141
1142 // Always a single 'type' instance
1143 *size = 1;
1144 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001145}
1146
Geoff Lange1a27752015-10-05 13:16:04 -04001147GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001148{
Jamie Madillc349ec02015-08-21 16:53:12 -04001149 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001150 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001151 return 0;
1152 }
1153
1154 GLint count = 0;
1155
Jamie Madill48ef11b2016-04-27 15:21:52 -04001156 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001157 {
1158 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001159 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001160
1161 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001162}
1163
Geoff Lange1a27752015-10-05 13:16:04 -04001164GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001165{
Jamie Madillc349ec02015-08-21 16:53:12 -04001166 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001167 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001168 return 0;
1169 }
1170
1171 size_t maxLength = 0;
1172
Jamie Madill48ef11b2016-04-27 15:21:52 -04001173 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001174 {
1175 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001176 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001177 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001178 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001179 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001180
Jamie Madillc349ec02015-08-21 16:53:12 -04001181 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001182}
1183
Geoff Lang7dd2e102014-11-10 15:19:26 -05001184GLint Program::getFragDataLocation(const std::string &name) const
1185{
1186 std::string baseName(name);
1187 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001188 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001189 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001190 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001191 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1192 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001193 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001194 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001195 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001196 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001197}
1198
Geoff Lange1a27752015-10-05 13:16:04 -04001199void Program::getActiveUniform(GLuint index,
1200 GLsizei bufsize,
1201 GLsizei *length,
1202 GLint *size,
1203 GLenum *type,
1204 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001205{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001206 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001207 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001208 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001209 ASSERT(index < mState.mUniforms.size());
1210 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211
1212 if (bufsize > 0)
1213 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001214 std::string string = uniform.name;
1215 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001216 {
1217 string += "[0]";
1218 }
1219
1220 strncpy(name, string.c_str(), bufsize);
1221 name[bufsize - 1] = '\0';
1222
1223 if (length)
1224 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001225 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001226 }
1227 }
1228
Jamie Madill62d31cb2015-09-11 13:25:51 -04001229 *size = uniform.elementCount();
1230 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001231 }
1232 else
1233 {
1234 if (bufsize > 0)
1235 {
1236 name[0] = '\0';
1237 }
1238
1239 if (length)
1240 {
1241 *length = 0;
1242 }
1243
1244 *size = 0;
1245 *type = GL_NONE;
1246 }
1247}
1248
Geoff Lange1a27752015-10-05 13:16:04 -04001249GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001250{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001251 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001252 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001253 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001254 }
1255 else
1256 {
1257 return 0;
1258 }
1259}
1260
Geoff Lange1a27752015-10-05 13:16:04 -04001261GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001262{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001263 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001264
1265 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001266 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001267 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001268 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001269 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001270 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001271 size_t length = uniform.name.length() + 1u;
1272 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001273 {
1274 length += 3; // Counting in "[0]".
1275 }
1276 maxLength = std::max(length, maxLength);
1277 }
1278 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001279 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280
Jamie Madill62d31cb2015-09-11 13:25:51 -04001281 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001282}
1283
1284GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1285{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001286 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
1287 const gl::LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001288 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001289 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1291 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1292 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1293 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1294 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1295 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1296 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1297 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1298 default:
1299 UNREACHABLE();
1300 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001301 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001302 return 0;
1303}
1304
1305bool Program::isValidUniformLocation(GLint location) const
1306{
Jamie Madille2e406c2016-06-02 13:04:10 -04001307 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001308 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1309 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001310}
1311
1312bool Program::isIgnoredUniformLocation(GLint location) const
1313{
1314 // Location is ignored if it is -1 or it was bound but non-existant in the shader or optimized
1315 // out
1316 return location == -1 ||
Jamie Madill48ef11b2016-04-27 15:21:52 -04001317 (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1318 mState.mUniformLocations[static_cast<size_t>(location)].ignored);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319}
1320
Jamie Madill62d31cb2015-09-11 13:25:51 -04001321const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001322{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001323 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1324 return mState.mUniforms[mState.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001325}
1326
Jamie Madill62d31cb2015-09-11 13:25:51 -04001327GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001328{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001329 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330}
1331
Jamie Madill62d31cb2015-09-11 13:25:51 -04001332GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001333{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001334 return mState.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001335}
1336
1337void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1338{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001339 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1340 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001341}
1342
1343void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1344{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001345 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1346 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347}
1348
1349void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1350{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001351 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1352 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353}
1354
1355void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1356{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001357 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1358 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359}
1360
1361void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1362{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001363 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1364 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365}
1366
1367void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1368{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001369 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1370 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371}
1372
1373void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1374{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001375 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1376 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377}
1378
1379void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1380{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001381 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1382 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383}
1384
1385void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1386{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001387 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1388 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001389}
1390
1391void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1392{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001393 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1394 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001395}
1396
1397void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1398{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001399 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1400 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401}
1402
1403void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1404{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001405 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1406 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001407}
1408
1409void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1410{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001411 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1412 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413}
1414
1415void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1416{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001417 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1418 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001419}
1420
1421void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1422{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001423 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1424 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425}
1426
1427void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1428{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001429 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1430 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431}
1432
1433void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1434{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001435 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1436 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437}
1438
1439void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1440{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001441 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1442 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443}
1444
1445void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1446{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001447 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1448 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449}
1450
1451void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1452{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001453 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1454 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001455}
1456
1457void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1458{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001459 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1460 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461}
1462
Geoff Lange1a27752015-10-05 13:16:04 -04001463void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001464{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001465 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001466}
1467
Geoff Lange1a27752015-10-05 13:16:04 -04001468void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001470 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001471}
1472
Geoff Lange1a27752015-10-05 13:16:04 -04001473void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001475 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001476}
1477
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001478void Program::flagForDeletion()
1479{
1480 mDeleteStatus = true;
1481}
1482
1483bool Program::isFlaggedForDeletion() const
1484{
1485 return mDeleteStatus;
1486}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001487
Brandon Jones43a53e22014-08-28 16:23:22 -07001488void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001489{
1490 mInfoLog.reset();
1491
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001493 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001494 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001495 }
1496 else
1497 {
Jamie Madillf6113162015-05-07 11:49:21 -04001498 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001499 }
1500}
1501
Geoff Lang7dd2e102014-11-10 15:19:26 -05001502bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1503{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001504 // Skip cache if we're using an infolog, so we get the full error.
1505 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1506 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1507 {
1508 return mCachedValidateSamplersResult.value();
1509 }
1510
1511 if (mTextureUnitTypesCache.empty())
1512 {
1513 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1514 }
1515 else
1516 {
1517 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1518 }
1519
1520 // if any two active samplers in a program are of different types, but refer to the same
1521 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1522 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1523 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1524 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1525 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001526 const LinkedUniform &uniform = mState.mUniforms[samplerIndex];
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001527 ASSERT(uniform.isSampler());
1528
1529 if (!uniform.staticUse)
1530 continue;
1531
1532 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1533 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1534
1535 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1536 {
1537 GLuint textureUnit = dataPtr[arrayElement];
1538
1539 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1540 {
1541 if (infoLog)
1542 {
1543 (*infoLog) << "Sampler uniform (" << textureUnit
1544 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1545 << caps.maxCombinedTextureImageUnits << ")";
1546 }
1547
1548 mCachedValidateSamplersResult = false;
1549 return false;
1550 }
1551
1552 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1553 {
1554 if (textureType != mTextureUnitTypesCache[textureUnit])
1555 {
1556 if (infoLog)
1557 {
1558 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1559 "image unit ("
1560 << textureUnit << ").";
1561 }
1562
1563 mCachedValidateSamplersResult = false;
1564 return false;
1565 }
1566 }
1567 else
1568 {
1569 mTextureUnitTypesCache[textureUnit] = textureType;
1570 }
1571 }
1572 }
1573
1574 mCachedValidateSamplersResult = true;
1575 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001576}
1577
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001578bool Program::isValidated() const
1579{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001580 return mValidated;
1581}
1582
Geoff Lange1a27752015-10-05 13:16:04 -04001583GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001584{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001585 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001586}
1587
1588void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1589{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001590 ASSERT(
1591 uniformBlockIndex <
1592 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001593
Jamie Madill48ef11b2016-04-27 15:21:52 -04001594 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001595
1596 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001597 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001598 std::string string = uniformBlock.name;
1599
Jamie Madill62d31cb2015-09-11 13:25:51 -04001600 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001601 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001602 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001603 }
1604
1605 strncpy(uniformBlockName, string.c_str(), bufSize);
1606 uniformBlockName[bufSize - 1] = '\0';
1607
1608 if (length)
1609 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001610 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001611 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001612 }
1613}
1614
Geoff Lange1a27752015-10-05 13:16:04 -04001615GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001616{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001617 int maxLength = 0;
1618
1619 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001620 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001621 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001622 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1623 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001624 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625 if (!uniformBlock.name.empty())
1626 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001627 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628
1629 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001630 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001631
1632 maxLength = std::max(length + arrayLength, maxLength);
1633 }
1634 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001635 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001636
1637 return maxLength;
1638}
1639
Geoff Lange1a27752015-10-05 13:16:04 -04001640GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001641{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001642 size_t subscript = GL_INVALID_INDEX;
1643 std::string baseName = gl::ParseUniformName(name, &subscript);
1644
Jamie Madill48ef11b2016-04-27 15:21:52 -04001645 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001646 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1647 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001648 const gl::UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001649 if (uniformBlock.name == baseName)
1650 {
1651 const bool arrayElementZero =
1652 (subscript == GL_INVALID_INDEX &&
1653 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1654 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1655 {
1656 return blockIndex;
1657 }
1658 }
1659 }
1660
1661 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001662}
1663
Jamie Madill62d31cb2015-09-11 13:25:51 -04001664const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001665{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001666 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1667 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001668}
1669
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001670void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1671{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001672 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001673 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001674}
1675
1676GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1677{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001678 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001679}
1680
1681void Program::resetUniformBlockBindings()
1682{
1683 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1684 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001685 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001686 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001687 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001688}
1689
Geoff Lang48dcae72014-02-05 16:28:24 -05001690void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1691{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001692 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001693 for (GLsizei i = 0; i < count; i++)
1694 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001695 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001696 }
1697
Jamie Madill48ef11b2016-04-27 15:21:52 -04001698 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001699}
1700
1701void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1702{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001703 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001704 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001705 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1706 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001707 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1708 if (length)
1709 {
1710 *length = lastNameIdx;
1711 }
1712 if (size)
1713 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001714 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001715 }
1716 if (type)
1717 {
1718 *type = varying.type;
1719 }
1720 if (name)
1721 {
1722 memcpy(name, varying.name.c_str(), lastNameIdx);
1723 name[lastNameIdx] = '\0';
1724 }
1725 }
1726}
1727
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001728GLsizei Program::getTransformFeedbackVaryingCount() const
1729{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001730 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001731 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001732 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001733 }
1734 else
1735 {
1736 return 0;
1737 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001738}
1739
1740GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1741{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001742 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001743 {
1744 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001745 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001746 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001747 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1748 }
1749
1750 return maxSize;
1751 }
1752 else
1753 {
1754 return 0;
1755 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001756}
1757
1758GLenum Program::getTransformFeedbackBufferMode() const
1759{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001760 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001761}
1762
Jamie Madillada9ecc2015-08-17 12:53:37 -04001763bool Program::linkVaryings(InfoLog &infoLog,
1764 const Shader *vertexShader,
Sami Väisänen46eaa942016-06-29 10:26:37 +03001765 const Shader *fragmentShader) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001766{
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001767 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1768
Jamie Madill4cff2472015-08-21 16:53:18 -04001769 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1770 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001771
Sami Väisänen46eaa942016-06-29 10:26:37 +03001772 std::map<GLuint, std::string> staticFragmentInputLocations;
1773
Jamie Madill4cff2472015-08-21 16:53:18 -04001774 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001776 bool matched = false;
1777
1778 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001779 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780 {
1781 continue;
1782 }
1783
Jamie Madill4cff2472015-08-21 16:53:18 -04001784 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001785 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001786 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001788 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001789 if (!linkValidateVaryings(infoLog, output.name, input, output,
1790 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791 {
1792 return false;
1793 }
1794
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795 matched = true;
1796 break;
1797 }
1798 }
1799
1800 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001801 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001803 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001804 return false;
1805 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001806
1807 // Check for aliased path rendering input bindings (if any).
1808 // If more than one binding refer statically to the same
1809 // location the link must fail.
1810
1811 if (!output.staticUse)
1812 continue;
1813
1814 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1815 if (inputBinding == -1)
1816 continue;
1817
1818 const auto it = staticFragmentInputLocations.find(inputBinding);
1819 if (it == std::end(staticFragmentInputLocations))
1820 {
1821 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1822 }
1823 else
1824 {
1825 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1826 << it->second;
1827 return false;
1828 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829 }
1830
Jamie Madillada9ecc2015-08-17 12:53:37 -04001831 // TODO(jmadill): verify no unmatched vertex varyings?
1832
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833 return true;
1834}
1835
Martin Radev4c4c8e72016-08-04 12:25:34 +03001836bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001837{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001838 // Check that uniforms defined in the vertex and fragment shaders are identical
1839 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001840 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1841 const std::vector<sh::Uniform> &fragmentUniforms =
1842 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001843
Jamie Madillea918db2015-08-18 14:48:59 -04001844 for (const sh::Uniform &vertexUniform : vertexUniforms)
1845 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001846 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001847 }
1848
1849 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1850 {
1851 auto entry = linkedUniforms.find(fragmentUniform.name);
1852 if (entry != linkedUniforms.end())
1853 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001854 LinkedUniform *vertexUniform = &entry->second;
1855 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1856 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001857 {
1858 return false;
1859 }
1860 }
1861 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001862 return true;
1863}
1864
1865bool Program::linkUniforms(gl::InfoLog &infoLog,
1866 const gl::Caps &caps,
1867 const Bindings &uniformBindings)
1868{
1869 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1870 {
1871 ASSERT(mState.mAttachedComputeShader == nullptr);
1872 if (!validateVertexAndFragmentUniforms(infoLog))
1873 {
1874 return false;
1875 }
1876 }
Jamie Madillea918db2015-08-18 14:48:59 -04001877
Jamie Madill62d31cb2015-09-11 13:25:51 -04001878 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1879 // Also check the maximum uniform vector and sampler counts.
1880 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1881 {
1882 return false;
1883 }
1884
Geoff Langd8605522016-04-13 10:19:12 -04001885 if (!indexUniforms(infoLog, caps, uniformBindings))
1886 {
1887 return false;
1888 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001889
Jamie Madillea918db2015-08-18 14:48:59 -04001890 return true;
1891}
1892
Geoff Langd8605522016-04-13 10:19:12 -04001893bool Program::indexUniforms(gl::InfoLog &infoLog,
1894 const gl::Caps &caps,
1895 const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001896{
Geoff Langd8605522016-04-13 10:19:12 -04001897 // Uniforms awaiting a location
1898 std::vector<VariableLocation> unboundUniforms;
1899 std::map<GLuint, VariableLocation> boundUniforms;
1900 int maxUniformLocation = -1;
1901
1902 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001903 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001904 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001905 const gl::LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001906
Geoff Langd8605522016-04-13 10:19:12 -04001907 if (uniform.isBuiltIn())
1908 {
1909 continue;
1910 }
1911
1912 int bindingLocation = uniformBindings.getBinding(uniform.name);
1913
1914 // Verify that this location isn't bound twice
1915 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
1916 {
1917 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
1918 return false;
1919 }
1920
Jamie Madill62d31cb2015-09-11 13:25:51 -04001921 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1922 {
Geoff Langd8605522016-04-13 10:19:12 -04001923 VariableLocation location(uniform.name, arrayIndex,
1924 static_cast<unsigned int>(uniformIndex));
1925
1926 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001927 {
Geoff Langd8605522016-04-13 10:19:12 -04001928 boundUniforms[bindingLocation] = location;
1929 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
1930 }
1931 else
1932 {
1933 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001934 }
1935 }
1936 }
Geoff Langd8605522016-04-13 10:19:12 -04001937
1938 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
1939 // not be assigned to those locations.
1940 std::set<GLuint> reservedLocations;
1941 for (const auto &binding : uniformBindings)
1942 {
1943 GLuint location = binding.second;
1944 if (boundUniforms.find(location) == boundUniforms.end())
1945 {
1946 reservedLocations.insert(location);
1947 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
1948 }
1949 }
1950
1951 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04001952 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04001953 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
1954 static_cast<size_t>(maxUniformLocation + 1)));
1955
1956 // Assign bound uniforms
1957 for (const auto &boundUniform : boundUniforms)
1958 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001959 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04001960 }
1961
1962 // Assign reserved uniforms
1963 for (const auto &reservedLocation : reservedLocations)
1964 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001965 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04001966 }
1967
1968 // Assign unbound uniforms
1969 size_t nextUniformLocation = 0;
1970 for (const auto &unboundUniform : unboundUniforms)
1971 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001972 while (mState.mUniformLocations[nextUniformLocation].used ||
1973 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04001974 {
1975 nextUniformLocation++;
1976 }
1977
Jamie Madill48ef11b2016-04-27 15:21:52 -04001978 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
1979 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04001980 nextUniformLocation++;
1981 }
1982
1983 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001984}
1985
Martin Radev4c4c8e72016-08-04 12:25:34 +03001986bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1987 const std::string &uniformName,
1988 const sh::InterfaceBlockField &vertexUniform,
1989 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001990{
Jamie Madillc4c744222015-11-04 09:39:47 -05001991 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1992 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001993 {
1994 return false;
1995 }
1996
1997 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1998 {
Jamie Madillf6113162015-05-07 11:49:21 -04001999 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002000 return false;
2001 }
2002
2003 return true;
2004}
2005
Jamie Madilleb979bf2016-11-15 12:28:46 -05002006// Assigns locations to all attributes from the bindings and program locations.
2007bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002008{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002009 const auto *vertexShader = mState.getAttachedVertexShader();
2010
Geoff Lang7dd2e102014-11-10 15:19:26 -05002011 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002012 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002013 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002014
2015 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002016 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002017 {
Jamie Madillf6113162015-05-07 11:49:21 -04002018 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002019 return false;
2020 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002021
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002022 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002023
Jamie Madillc349ec02015-08-21 16:53:12 -04002024 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002025 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002026 {
2027 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002028 ASSERT(attribute.staticUse);
2029
Jamie Madilleb979bf2016-11-15 12:28:46 -05002030 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002031 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002032 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002033 attribute.location = bindingLocation;
2034 }
2035
2036 if (attribute.location != -1)
2037 {
2038 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002039 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040
Jamie Madill63805b42015-08-25 13:17:39 -04002041 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042 {
Jamie Madillf6113162015-05-07 11:49:21 -04002043 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002044 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045
2046 return false;
2047 }
2048
Jamie Madill63805b42015-08-25 13:17:39 -04002049 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 {
Jamie Madill63805b42015-08-25 13:17:39 -04002051 const int regLocation = attribute.location + reg;
2052 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053
2054 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002055 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002056 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002058 // TODO(jmadill): fix aliasing on ES2
2059 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002060 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002061 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002062 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063 return false;
2064 }
2065 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002066 else
2067 {
Jamie Madill63805b42015-08-25 13:17:39 -04002068 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002069 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002070
Jamie Madill63805b42015-08-25 13:17:39 -04002071 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002072 }
2073 }
2074 }
2075
2076 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002077 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079 ASSERT(attribute.staticUse);
2080
Jamie Madillc349ec02015-08-21 16:53:12 -04002081 // Not set by glBindAttribLocation or by location layout qualifier
2082 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002083 {
Jamie Madill63805b42015-08-25 13:17:39 -04002084 int regs = VariableRegisterCount(attribute.type);
2085 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086
Jamie Madill63805b42015-08-25 13:17:39 -04002087 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088 {
Jamie Madillf6113162015-05-07 11:49:21 -04002089 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002090 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091 }
2092
Jamie Madillc349ec02015-08-21 16:53:12 -04002093 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002094 }
2095 }
2096
Jamie Madill48ef11b2016-04-27 15:21:52 -04002097 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002098 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002099 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002100 ASSERT(attribute.location != -1);
2101 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002102
Jamie Madill63805b42015-08-25 13:17:39 -04002103 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002104 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002105 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002106 }
2107 }
2108
Geoff Lang7dd2e102014-11-10 15:19:26 -05002109 return true;
2110}
2111
Martin Radev4c4c8e72016-08-04 12:25:34 +03002112bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2113 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2114 const std::string &errorMessage,
2115 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002116{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002117 GLuint blockCount = 0;
2118 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002119 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002120 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002121 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002122 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002123 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002124 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002125 return false;
2126 }
2127 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002128 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002129 return true;
2130}
Jamie Madille473dee2015-08-18 14:49:01 -04002131
Martin Radev4c4c8e72016-08-04 12:25:34 +03002132bool Program::validateVertexAndFragmentInterfaceBlocks(
2133 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2134 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2135 InfoLog &infoLog) const
2136{
2137 // Check that interface blocks defined in the vertex and fragment shaders are identical
2138 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2139 UniformBlockMap linkedUniformBlocks;
2140
2141 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2142 {
2143 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2144 }
2145
Jamie Madille473dee2015-08-18 14:49:01 -04002146 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002147 {
Jamie Madille473dee2015-08-18 14:49:01 -04002148 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149 if (entry != linkedUniformBlocks.end())
2150 {
2151 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2152 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2153 {
2154 return false;
2155 }
2156 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002157 }
2158 return true;
2159}
Jamie Madille473dee2015-08-18 14:49:01 -04002160
Martin Radev4c4c8e72016-08-04 12:25:34 +03002161bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2162{
2163 if (mState.mAttachedComputeShader)
2164 {
2165 const Shader &computeShader = *mState.mAttachedComputeShader;
2166 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2167
2168 if (!validateUniformBlocksCount(
2169 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2170 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2171 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002173 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002174 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002175 return true;
2176 }
2177
2178 const Shader &vertexShader = *mState.mAttachedVertexShader;
2179 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2180
2181 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2182 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2183
2184 if (!validateUniformBlocksCount(
2185 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2186 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2187 {
2188 return false;
2189 }
2190 if (!validateUniformBlocksCount(
2191 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2192 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2193 infoLog))
2194 {
2195
2196 return false;
2197 }
2198 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2199 infoLog))
2200 {
2201 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002202 }
Jamie Madille473dee2015-08-18 14:49:01 -04002203
Geoff Lang7dd2e102014-11-10 15:19:26 -05002204 return true;
2205}
2206
Martin Radev4c4c8e72016-08-04 12:25:34 +03002207bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog,
2208 const sh::InterfaceBlock &vertexInterfaceBlock,
2209 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002210{
2211 const char* blockName = vertexInterfaceBlock.name.c_str();
2212 // validate blocks for the same member types
2213 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2214 {
Jamie Madillf6113162015-05-07 11:49:21 -04002215 infoLog << "Types for interface block '" << blockName
2216 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002217 return false;
2218 }
2219 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2220 {
Jamie Madillf6113162015-05-07 11:49:21 -04002221 infoLog << "Array sizes differ for interface block '" << blockName
2222 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002223 return false;
2224 }
2225 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2226 {
Jamie Madillf6113162015-05-07 11:49:21 -04002227 infoLog << "Layout qualifiers differ for interface block '" << blockName
2228 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002229 return false;
2230 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002231 const unsigned int numBlockMembers =
2232 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002233 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2234 {
2235 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2236 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2237 if (vertexMember.name != fragmentMember.name)
2238 {
Jamie Madillf6113162015-05-07 11:49:21 -04002239 infoLog << "Name mismatch for field " << blockMemberIndex
2240 << " of interface block '" << blockName
2241 << "': (in vertex: '" << vertexMember.name
2242 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002243 return false;
2244 }
2245 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2246 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2247 {
2248 return false;
2249 }
2250 }
2251 return true;
2252}
2253
2254bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2255 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2256{
2257 if (vertexVariable.type != fragmentVariable.type)
2258 {
Jamie Madillf6113162015-05-07 11:49:21 -04002259 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002260 return false;
2261 }
2262 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2263 {
Jamie Madillf6113162015-05-07 11:49:21 -04002264 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002265 return false;
2266 }
2267 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2268 {
Jamie Madillf6113162015-05-07 11:49:21 -04002269 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002270 return false;
2271 }
2272
2273 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2274 {
Jamie Madillf6113162015-05-07 11:49:21 -04002275 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002276 return false;
2277 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002278 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002279 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2280 {
2281 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2282 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2283
2284 if (vertexMember.name != fragmentMember.name)
2285 {
Jamie Madillf6113162015-05-07 11:49:21 -04002286 infoLog << "Name mismatch for field '" << memberIndex
2287 << "' of " << variableName
2288 << ": (in vertex: '" << vertexMember.name
2289 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002290 return false;
2291 }
2292
2293 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2294 vertexMember.name + "'";
2295
2296 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2297 {
2298 return false;
2299 }
2300 }
2301
2302 return true;
2303}
2304
2305bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2306{
Cooper Partin1acf4382015-06-12 12:38:57 -07002307#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2308 const bool validatePrecision = true;
2309#else
2310 const bool validatePrecision = false;
2311#endif
2312
2313 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002314 {
2315 return false;
2316 }
2317
2318 return true;
2319}
2320
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002321bool Program::linkValidateVaryings(InfoLog &infoLog,
2322 const std::string &varyingName,
2323 const sh::Varying &vertexVarying,
2324 const sh::Varying &fragmentVarying,
2325 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002326{
2327 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2328 {
2329 return false;
2330 }
2331
Jamie Madille9cc4692015-02-19 16:00:13 -05002332 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002333 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002334 infoLog << "Interpolation types for " << varyingName
2335 << " differ between vertex and fragment shaders.";
2336 return false;
2337 }
2338
2339 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2340 {
2341 infoLog << "Invariance for " << varyingName
2342 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002343 return false;
2344 }
2345
2346 return true;
2347}
2348
Jamie Madillccdf74b2015-08-18 10:46:12 -04002349bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2350 const std::vector<const sh::Varying *> &varyings,
2351 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002352{
2353 size_t totalComponents = 0;
2354
Jamie Madillccdf74b2015-08-18 10:46:12 -04002355 std::set<std::string> uniqueNames;
2356
Jamie Madill48ef11b2016-04-27 15:21:52 -04002357 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002358 {
2359 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002360 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002361 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002362 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002363 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002364 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002365 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002366 infoLog << "Two transform feedback varyings specify the same output variable ("
2367 << tfVaryingName << ").";
2368 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002369 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002370 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002371
Geoff Lang1a683462015-09-29 15:09:59 -04002372 if (varying->isArray())
2373 {
2374 infoLog << "Capture of arrays is undefined and not supported.";
2375 return false;
2376 }
2377
Jamie Madillccdf74b2015-08-18 10:46:12 -04002378 // TODO(jmadill): Investigate implementation limits on D3D11
2379 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002380 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002381 componentCount > caps.maxTransformFeedbackSeparateComponents)
2382 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002383 infoLog << "Transform feedback varying's " << varying->name << " components ("
2384 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002385 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002386 return false;
2387 }
2388
2389 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002390 found = true;
2391 break;
2392 }
2393 }
2394
Jamie Madill89bb70e2015-08-31 14:18:39 -04002395 if (tfVaryingName.find('[') != std::string::npos)
2396 {
Geoff Lang1a683462015-09-29 15:09:59 -04002397 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002398 return false;
2399 }
2400
Geoff Lang7dd2e102014-11-10 15:19:26 -05002401 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2402 ASSERT(found);
2403 }
2404
Jamie Madill48ef11b2016-04-27 15:21:52 -04002405 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002406 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002407 {
Jamie Madillf6113162015-05-07 11:49:21 -04002408 infoLog << "Transform feedback varying total components (" << totalComponents
2409 << ") exceed the maximum interleaved components ("
2410 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411 return false;
2412 }
2413
2414 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002415}
2416
Jamie Madillccdf74b2015-08-18 10:46:12 -04002417void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2418{
2419 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002420 mState.mTransformFeedbackVaryingVars.clear();
2421 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002422 {
2423 for (const sh::Varying *varying : varyings)
2424 {
2425 if (tfVaryingName == varying->name)
2426 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002427 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002428 break;
2429 }
2430 }
2431 }
2432}
2433
2434std::vector<const sh::Varying *> Program::getMergedVaryings() const
2435{
2436 std::set<std::string> uniqueNames;
2437 std::vector<const sh::Varying *> varyings;
2438
Jamie Madill48ef11b2016-04-27 15:21:52 -04002439 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002440 {
2441 if (uniqueNames.count(varying.name) == 0)
2442 {
2443 uniqueNames.insert(varying.name);
2444 varyings.push_back(&varying);
2445 }
2446 }
2447
Jamie Madill48ef11b2016-04-27 15:21:52 -04002448 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002449 {
2450 if (uniqueNames.count(varying.name) == 0)
2451 {
2452 uniqueNames.insert(varying.name);
2453 varyings.push_back(&varying);
2454 }
2455 }
2456
2457 return varyings;
2458}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002459
2460void Program::linkOutputVariables()
2461{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002462 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002463 ASSERT(fragmentShader != nullptr);
2464
2465 // Skip this step for GLES2 shaders.
2466 if (fragmentShader->getShaderVersion() == 100)
2467 return;
2468
Jamie Madilla0a9e122015-09-02 15:54:30 -04002469 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002470
2471 // TODO(jmadill): any caps validation here?
2472
2473 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2474 outputVariableIndex++)
2475 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002476 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002477
2478 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2479 if (outputVariable.isBuiltIn())
2480 continue;
2481
2482 // Since multiple output locations must be specified, use 0 for non-specified locations.
2483 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2484
2485 ASSERT(outputVariable.staticUse);
2486
2487 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2488 elementIndex++)
2489 {
2490 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002491 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002492 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002493 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002494 VariableLocation(outputVariable.name, element, outputVariableIndex);
2495 }
2496 }
2497}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002498
Martin Radev4c4c8e72016-08-04 12:25:34 +03002499bool Program::flattenUniformsAndCheckCapsForShader(const gl::Shader &shader,
2500 GLuint maxUniformComponents,
2501 GLuint maxTextureImageUnits,
2502 const std::string &componentsErrorMessage,
2503 const std::string &samplerErrorMessage,
2504 std::vector<LinkedUniform> &samplerUniforms,
2505 InfoLog &infoLog)
2506{
2507 VectorAndSamplerCount vasCount;
2508 for (const sh::Uniform &uniform : shader.getUniforms())
2509 {
2510 if (uniform.staticUse)
2511 {
2512 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2513 }
2514 }
2515
2516 if (vasCount.vectorCount > maxUniformComponents)
2517 {
2518 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2519 return false;
2520 }
2521
2522 if (vasCount.samplerCount > maxTextureImageUnits)
2523 {
2524 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2525 return false;
2526 }
2527
2528 return true;
2529}
2530
Jamie Madill62d31cb2015-09-11 13:25:51 -04002531bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2532{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002533 std::vector<LinkedUniform> samplerUniforms;
2534
Martin Radev4c4c8e72016-08-04 12:25:34 +03002535 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002536 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002537 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2538
2539 // TODO (mradev): check whether we need finer-grained component counting
2540 if (!flattenUniformsAndCheckCapsForShader(
2541 *computeShader, caps.maxComputeUniformComponents / 4,
2542 caps.maxComputeTextureImageUnits,
2543 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2544 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2545 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002546 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002547 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002548 }
2549 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002550 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002551 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002552 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002553
Martin Radev4c4c8e72016-08-04 12:25:34 +03002554 if (!flattenUniformsAndCheckCapsForShader(
2555 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2556 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2557 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2558 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002559 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002560 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002561 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002562 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002563
Martin Radev4c4c8e72016-08-04 12:25:34 +03002564 if (!flattenUniformsAndCheckCapsForShader(
2565 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2566 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2567 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2568 infoLog))
2569 {
2570 return false;
2571 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002572 }
2573
Jamie Madill48ef11b2016-04-27 15:21:52 -04002574 mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002575 mSamplerUniformRange.end =
2576 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2577
Jamie Madill48ef11b2016-04-27 15:21:52 -04002578 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002579
Jamie Madill62d31cb2015-09-11 13:25:51 -04002580 return true;
2581}
2582
2583Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002584 const std::string &fullName,
2585 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002586{
2587 VectorAndSamplerCount vectorAndSamplerCount;
2588
2589 if (uniform.isStruct())
2590 {
2591 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2592 {
2593 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2594
2595 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2596 {
2597 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2598 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2599
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002600 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002601 }
2602 }
2603
2604 return vectorAndSamplerCount;
2605 }
2606
2607 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002608 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002609 if (!UniformInList(mState.getUniforms(), fullName) &&
2610 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002611 {
2612 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2613 uniform.arraySize, -1,
2614 sh::BlockMemberInfo::getDefaultBlockInfo());
2615 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002616
2617 // Store sampler uniforms separately, so we'll append them to the end of the list.
2618 if (isSampler)
2619 {
2620 samplerUniforms->push_back(linkedUniform);
2621 }
2622 else
2623 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002624 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002625 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002626 }
2627
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002628 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002629
2630 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2631 // Likewise, don't count "real" uniforms towards sampler count.
2632 vectorAndSamplerCount.vectorCount =
2633 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002634 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002635
2636 return vectorAndSamplerCount;
2637}
2638
2639void Program::gatherInterfaceBlockInfo()
2640{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002641 ASSERT(mState.mUniformBlocks.empty());
2642
2643 if (mState.mAttachedComputeShader)
2644 {
2645 const gl::Shader *computeShader = mState.getAttachedComputeShader();
2646
2647 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2648 {
2649
2650 // Only 'packed' blocks are allowed to be considered inactive.
2651 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2652 continue;
2653
2654 for (gl::UniformBlock &block : mState.mUniformBlocks)
2655 {
2656 if (block.name == computeBlock.name)
2657 {
2658 block.computeStaticUse = computeBlock.staticUse;
2659 }
2660 }
2661
2662 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2663 }
2664 return;
2665 }
2666
Jamie Madill62d31cb2015-09-11 13:25:51 -04002667 std::set<std::string> visitedList;
2668
Jamie Madill48ef11b2016-04-27 15:21:52 -04002669 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002670
Jamie Madill62d31cb2015-09-11 13:25:51 -04002671 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2672 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002673 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002674 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2675 continue;
2676
2677 if (visitedList.count(vertexBlock.name) > 0)
2678 continue;
2679
2680 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2681 visitedList.insert(vertexBlock.name);
2682 }
2683
Jamie Madill48ef11b2016-04-27 15:21:52 -04002684 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002685
2686 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2687 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002688 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002689 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2690 continue;
2691
2692 if (visitedList.count(fragmentBlock.name) > 0)
2693 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002694 for (gl::UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002695 {
2696 if (block.name == fragmentBlock.name)
2697 {
2698 block.fragmentStaticUse = fragmentBlock.staticUse;
2699 }
2700 }
2701
2702 continue;
2703 }
2704
2705 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2706 visitedList.insert(fragmentBlock.name);
2707 }
2708}
2709
Jamie Madill4a3c2342015-10-08 12:58:45 -04002710template <typename VarT>
2711void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2712 const std::string &prefix,
2713 int blockIndex)
2714{
2715 for (const VarT &field : fields)
2716 {
2717 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2718
2719 if (field.isStruct())
2720 {
2721 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2722 {
2723 const std::string uniformElementName =
2724 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2725 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2726 }
2727 }
2728 else
2729 {
2730 // If getBlockMemberInfo returns false, the uniform is optimized out.
2731 sh::BlockMemberInfo memberInfo;
2732 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2733 {
2734 continue;
2735 }
2736
2737 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2738 blockIndex, memberInfo);
2739
2740 // Since block uniforms have no location, we don't need to store them in the uniform
2741 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002742 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002743 }
2744 }
2745}
2746
Jamie Madill62d31cb2015-09-11 13:25:51 -04002747void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2748{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002749 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002750 size_t blockSize = 0;
2751
2752 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002753 std::stringstream blockNameStr;
2754 blockNameStr << interfaceBlock.name;
2755 if (interfaceBlock.arraySize > 0)
2756 {
2757 blockNameStr << "[0]";
2758 }
2759 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002760 {
2761 return;
2762 }
2763
2764 // Track the first and last uniform index to determine the range of active uniforms in the
2765 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002766 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002767 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002768 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002769
2770 std::vector<unsigned int> blockUniformIndexes;
2771 for (size_t blockUniformIndex = firstBlockUniformIndex;
2772 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2773 {
2774 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2775 }
2776
2777 if (interfaceBlock.arraySize > 0)
2778 {
2779 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2780 {
2781 UniformBlock block(interfaceBlock.name, true, arrayElement);
2782 block.memberUniformIndexes = blockUniformIndexes;
2783
Martin Radev4c4c8e72016-08-04 12:25:34 +03002784 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002785 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002786 case GL_VERTEX_SHADER:
2787 {
2788 block.vertexStaticUse = interfaceBlock.staticUse;
2789 break;
2790 }
2791 case GL_FRAGMENT_SHADER:
2792 {
2793 block.fragmentStaticUse = interfaceBlock.staticUse;
2794 break;
2795 }
2796 case GL_COMPUTE_SHADER:
2797 {
2798 block.computeStaticUse = interfaceBlock.staticUse;
2799 break;
2800 }
2801 default:
2802 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002803 }
2804
Qin Jiajia0350a642016-11-01 17:01:51 +08002805 // Since all block elements in an array share the same active uniforms, they will all be
2806 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2807 // here we will add every block element in the array.
2808 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002809 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002810 }
2811 }
2812 else
2813 {
2814 UniformBlock block(interfaceBlock.name, false, 0);
2815 block.memberUniformIndexes = blockUniformIndexes;
2816
Martin Radev4c4c8e72016-08-04 12:25:34 +03002817 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002818 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002819 case GL_VERTEX_SHADER:
2820 {
2821 block.vertexStaticUse = interfaceBlock.staticUse;
2822 break;
2823 }
2824 case GL_FRAGMENT_SHADER:
2825 {
2826 block.fragmentStaticUse = interfaceBlock.staticUse;
2827 break;
2828 }
2829 case GL_COMPUTE_SHADER:
2830 {
2831 block.computeStaticUse = interfaceBlock.staticUse;
2832 break;
2833 }
2834 default:
2835 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002836 }
2837
Jamie Madill4a3c2342015-10-08 12:58:45 -04002838 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002839 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002840 }
2841}
2842
2843template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002844GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002845{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002846 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2847 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002848 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2849
Corentin Wallez15ac5342016-11-03 17:06:39 -04002850 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2851 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2852 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002853 GLsizei maxElementCount =
2854 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2855
2856 GLsizei count = countIn;
2857 GLsizei clampedCount = count * vectorSize;
2858 if (clampedCount > maxElementCount)
2859 {
2860 clampedCount = maxElementCount;
2861 count = maxElementCount / vectorSize;
2862 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002863
Jamie Madill62d31cb2015-09-11 13:25:51 -04002864 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2865 {
2866 // Do a cast conversion for boolean types. From the spec:
2867 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2868 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002869 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002870 {
2871 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2872 }
2873 }
2874 else
2875 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002876 // Invalide the validation cache if we modify the sampler data.
Corentin Wallez15ac5342016-11-03 17:06:39 -04002877 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * clampedCount) != 0)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002878 {
2879 mCachedValidateSamplersResult.reset();
2880 }
2881
Corentin Wallez15ac5342016-11-03 17:06:39 -04002882 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002883 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002884
2885 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002886}
2887
2888template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002889GLsizei Program::setMatrixUniformInternal(GLint location,
2890 GLsizei count,
2891 GLboolean transpose,
2892 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002893{
2894 if (!transpose)
2895 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002896 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002897 }
2898
2899 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002900 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2901 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002902 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002903
2904 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2905 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2906 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2907 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2908
2909 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002910 {
2911 size_t elementOffset = element * rows * cols;
2912
2913 for (size_t row = 0; row < rows; ++row)
2914 {
2915 for (size_t col = 0; col < cols; ++col)
2916 {
2917 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2918 }
2919 }
2920 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002921
2922 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002923}
2924
2925template <typename DestT>
2926void Program::getUniformInternal(GLint location, DestT *dataOut) const
2927{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002928 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2929 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002930
2931 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2932
2933 GLenum componentType = VariableComponentType(uniform.type);
2934 if (componentType == GLTypeToGLenum<DestT>::value)
2935 {
2936 memcpy(dataOut, srcPointer, uniform.getElementSize());
2937 return;
2938 }
2939
Corentin Wallez6596c462016-03-17 17:26:58 -04002940 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002941
2942 switch (componentType)
2943 {
2944 case GL_INT:
2945 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2946 break;
2947 case GL_UNSIGNED_INT:
2948 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2949 break;
2950 case GL_BOOL:
2951 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2952 break;
2953 case GL_FLOAT:
2954 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2955 break;
2956 default:
2957 UNREACHABLE();
2958 }
2959}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002960}