blob: b1f7abff6df44c6e2f3f27f4c26c51604cf606c4 [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"
Geoff Lang7dd2e102014-11-10 15:19:26 -050020#include "libANGLE/Data.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"
Geoff Lang2b5420c2014-11-19 14:20:15 -050023#include "libANGLE/renderer/Renderer.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"
Geoff Lang7dd2e102014-11-10 15:19:26 -050026
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027namespace gl
28{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000029
Geoff Lang7dd2e102014-11-10 15:19:26 -050030namespace
31{
32
Jamie Madill62d31cb2015-09-11 13:25:51 -040033void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
34{
35 stream->writeInt(var.type);
36 stream->writeInt(var.precision);
37 stream->writeString(var.name);
38 stream->writeString(var.mappedName);
39 stream->writeInt(var.arraySize);
40 stream->writeInt(var.staticUse);
41 stream->writeString(var.structName);
42 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050043}
44
Jamie Madill62d31cb2015-09-11 13:25:51 -040045void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
46{
47 var->type = stream->readInt<GLenum>();
48 var->precision = stream->readInt<GLenum>();
49 var->name = stream->readString();
50 var->mappedName = stream->readString();
51 var->arraySize = stream->readInt<unsigned int>();
52 var->staticUse = stream->readBool();
53 var->structName = stream->readString();
54}
55
Jamie Madill62d31cb2015-09-11 13:25:51 -040056// This simplified cast function doesn't need to worry about advanced concepts like
57// depth range values, or casting to bool.
58template <typename DestT, typename SrcT>
59DestT UniformStateQueryCast(SrcT value);
60
61// From-Float-To-Integer Casts
62template <>
63GLint UniformStateQueryCast(GLfloat value)
64{
65 return clampCast<GLint>(roundf(value));
66}
67
68template <>
69GLuint UniformStateQueryCast(GLfloat value)
70{
71 return clampCast<GLuint>(roundf(value));
72}
73
74// From-Integer-to-Integer Casts
75template <>
76GLint UniformStateQueryCast(GLuint value)
77{
78 return clampCast<GLint>(value);
79}
80
81template <>
82GLuint UniformStateQueryCast(GLint value)
83{
84 return clampCast<GLuint>(value);
85}
86
87// From-Boolean-to-Anything Casts
88template <>
89GLfloat UniformStateQueryCast(GLboolean value)
90{
91 return (value == GL_TRUE ? 1.0f : 0.0f);
92}
93
94template <>
95GLint UniformStateQueryCast(GLboolean value)
96{
97 return (value == GL_TRUE ? 1 : 0);
98}
99
100template <>
101GLuint UniformStateQueryCast(GLboolean value)
102{
103 return (value == GL_TRUE ? 1u : 0u);
104}
105
106// Default to static_cast
107template <typename DestT, typename SrcT>
108DestT UniformStateQueryCast(SrcT value)
109{
110 return static_cast<DestT>(value);
111}
112
113template <typename SrcT, typename DestT>
114void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
115{
116 for (int comp = 0; comp < components; ++comp)
117 {
118 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
119 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
120 size_t offset = comp * 4;
121 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
122 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
123 }
124}
125
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400126bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
127{
128 for (const LinkedUniform &uniform : list)
129 {
130 if (uniform.name == name)
131 return true;
132 }
133
134 return false;
135}
136
Jamie Madill62d31cb2015-09-11 13:25:51 -0400137} // anonymous namespace
138
Jamie Madill4a3c2342015-10-08 12:58:45 -0400139const char *const g_fakepath = "C:\\fakepath";
140
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400141InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000142{
143}
144
145InfoLog::~InfoLog()
146{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000147}
148
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400149size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000150{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400151 const std::string &logString = mStream.str();
152 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000153}
154
Geoff Lange1a27752015-10-05 13:16:04 -0400155void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000156{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400157 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000158
159 if (bufSize > 0)
160 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400161 const std::string str(mStream.str());
162
163 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000164 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400165 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
166 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000167 }
168
169 infoLog[index] = '\0';
170 }
171
172 if (length)
173 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400174 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000175 }
176}
177
178// append a santized message to the program info log.
179// The D3D compiler includes a fake file path in some of the warning or error
180// messages, so lets remove all occurrences of this fake file path from the log.
181void InfoLog::appendSanitized(const char *message)
182{
183 std::string msg(message);
184
185 size_t found;
186 do
187 {
188 found = msg.find(g_fakepath);
189 if (found != std::string::npos)
190 {
191 msg.erase(found, strlen(g_fakepath));
192 }
193 }
194 while (found != std::string::npos);
195
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400196 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000197}
198
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000199void InfoLog::reset()
200{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000201}
202
Geoff Langd8605522016-04-13 10:19:12 -0400203VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000204{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500205}
206
Geoff Langd8605522016-04-13 10:19:12 -0400207VariableLocation::VariableLocation(const std::string &name,
208 unsigned int element,
209 unsigned int index)
210 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500211{
212}
213
Geoff Langd8605522016-04-13 10:19:12 -0400214void Program::Bindings::bindLocation(GLuint index, const std::string &name)
215{
216 mBindings[name] = index;
217}
218
219int Program::Bindings::getBinding(const std::string &name) const
220{
221 auto iter = mBindings.find(name);
222 return (iter != mBindings.end()) ? iter->second : -1;
223}
224
225Program::Bindings::const_iterator Program::Bindings::begin() const
226{
227 return mBindings.begin();
228}
229
230Program::Bindings::const_iterator Program::Bindings::end() const
231{
232 return mBindings.end();
233}
234
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400235Program::Data::Data()
Geoff Lang70d0f492015-12-10 17:45:46 -0500236 : mLabel(),
237 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400238 mAttachedVertexShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500239 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
240 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400241{
242}
243
244Program::Data::~Data()
245{
246 if (mAttachedVertexShader != nullptr)
247 {
248 mAttachedVertexShader->release();
249 }
250
251 if (mAttachedFragmentShader != nullptr)
252 {
253 mAttachedFragmentShader->release();
254 }
255}
256
Geoff Lang70d0f492015-12-10 17:45:46 -0500257const std::string &Program::Data::getLabel()
258{
259 return mLabel;
260}
261
Jamie Madill62d31cb2015-09-11 13:25:51 -0400262const LinkedUniform *Program::Data::getUniformByName(const std::string &name) const
263{
264 for (const LinkedUniform &linkedUniform : mUniforms)
265 {
266 if (linkedUniform.name == name)
267 {
268 return &linkedUniform;
269 }
270 }
271
272 return nullptr;
273}
274
275GLint Program::Data::getUniformLocation(const std::string &name) const
276{
277 size_t subscript = GL_INVALID_INDEX;
278 std::string baseName = gl::ParseUniformName(name, &subscript);
279
280 for (size_t location = 0; location < mUniformLocations.size(); ++location)
281 {
282 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400283 if (!uniformLocation.used)
284 {
285 continue;
286 }
287
288 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400289
290 if (uniform.name == baseName)
291 {
Geoff Langd8605522016-04-13 10:19:12 -0400292 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400293 {
Geoff Langd8605522016-04-13 10:19:12 -0400294 if (uniformLocation.element == subscript ||
295 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
296 {
297 return static_cast<GLint>(location);
298 }
299 }
300 else
301 {
302 if (subscript == GL_INVALID_INDEX)
303 {
304 return static_cast<GLint>(location);
305 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400306 }
307 }
308 }
309
310 return -1;
311}
312
313GLuint Program::Data::getUniformIndex(const std::string &name) const
314{
315 size_t subscript = GL_INVALID_INDEX;
316 std::string baseName = gl::ParseUniformName(name, &subscript);
317
318 // The app is not allowed to specify array indices other than 0 for arrays of basic types
319 if (subscript != 0 && subscript != GL_INVALID_INDEX)
320 {
321 return GL_INVALID_INDEX;
322 }
323
324 for (size_t index = 0; index < mUniforms.size(); index++)
325 {
326 const LinkedUniform &uniform = mUniforms[index];
327 if (uniform.name == baseName)
328 {
329 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
330 {
331 return static_cast<GLuint>(index);
332 }
333 }
334 }
335
336 return GL_INVALID_INDEX;
337}
338
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400339Program::Program(rx::ImplFactory *factory, ResourceManager *manager, GLuint handle)
340 : mProgram(factory->createProgram(mData)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400341 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500342 mLinked(false),
343 mDeleteStatus(false),
344 mRefCount(0),
345 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400346 mHandle(handle),
347 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500348{
349 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000350
351 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500352 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353}
354
355Program::~Program()
356{
357 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000358
Geoff Lang7dd2e102014-11-10 15:19:26 -0500359 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
Geoff Lang70d0f492015-12-10 17:45:46 -0500362void Program::setLabel(const std::string &label)
363{
364 mData.mLabel = label;
365}
366
367const std::string &Program::getLabel() const
368{
369 return mData.mLabel;
370}
371
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372bool Program::attachShader(Shader *shader)
373{
374 if (shader->getType() == GL_VERTEX_SHADER)
375 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400376 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377 {
378 return false;
379 }
380
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400381 mData.mAttachedVertexShader = shader;
382 mData.mAttachedVertexShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383 }
384 else if (shader->getType() == GL_FRAGMENT_SHADER)
385 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400386 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387 {
388 return false;
389 }
390
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400391 mData.mAttachedFragmentShader = shader;
392 mData.mAttachedFragmentShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393 }
394 else UNREACHABLE();
395
396 return true;
397}
398
399bool Program::detachShader(Shader *shader)
400{
401 if (shader->getType() == GL_VERTEX_SHADER)
402 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400403 if (mData.mAttachedVertexShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404 {
405 return false;
406 }
407
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400408 shader->release();
409 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410 }
411 else if (shader->getType() == GL_FRAGMENT_SHADER)
412 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400413 if (mData.mAttachedFragmentShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414 {
415 return false;
416 }
417
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400418 shader->release();
419 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420 }
421 else UNREACHABLE();
422
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423 return true;
424}
425
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000426int Program::getAttachedShadersCount() const
427{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400428 return (mData.mAttachedVertexShader ? 1 : 0) + (mData.mAttachedFragmentShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000429}
430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431void Program::bindAttributeLocation(GLuint index, const char *name)
432{
Geoff Langd8605522016-04-13 10:19:12 -0400433 mAttributeBindings.bindLocation(index, name);
434}
435
436void Program::bindUniformLocation(GLuint index, const char *name)
437{
438 // Bind the base uniform name only since array indices other than 0 cannot be bound
439 mUniformBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440}
441
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
443// compiling them into binaries, determining the attribute mappings, and collecting
444// a list of uniforms
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400445Error Program::link(const gl::Data &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000446{
447 unlink(false);
448
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000449 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000450 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000451
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400452 if (!mData.mAttachedFragmentShader || !mData.mAttachedFragmentShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500453 {
454 return Error(GL_NO_ERROR);
455 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400456 ASSERT(mData.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500457
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400458 if (!mData.mAttachedVertexShader || !mData.mAttachedVertexShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500459 {
460 return Error(GL_NO_ERROR);
461 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400462 ASSERT(mData.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500463
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400464 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mData.mAttachedVertexShader))
Jamie Madill437d2662014-12-05 14:23:35 -0500465 {
466 return Error(GL_NO_ERROR);
467 }
468
Jamie Madillada9ecc2015-08-17 12:53:37 -0400469 if (!linkVaryings(mInfoLog, mData.mAttachedVertexShader, mData.mAttachedFragmentShader))
470 {
471 return Error(GL_NO_ERROR);
472 }
473
Geoff Langd8605522016-04-13 10:19:12 -0400474 if (!linkUniforms(mInfoLog, *data.caps, mUniformBindings))
Jamie Madillea918db2015-08-18 14:48:59 -0400475 {
476 return Error(GL_NO_ERROR);
477 }
478
Jamie Madille473dee2015-08-18 14:49:01 -0400479 if (!linkUniformBlocks(mInfoLog, *data.caps))
480 {
481 return Error(GL_NO_ERROR);
482 }
483
Jamie Madillccdf74b2015-08-18 10:46:12 -0400484 const auto &mergedVaryings = getMergedVaryings();
485
486 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, *data.caps))
487 {
488 return Error(GL_NO_ERROR);
489 }
490
Jamie Madill80a6fc02015-08-21 16:53:16 -0400491 linkOutputVariables();
492
Jamie Madillf5f4ad22015-09-02 18:32:38 +0000493 rx::LinkResult result = mProgram->link(data, mInfoLog);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500494 if (result.error.isError() || !result.linkSuccess)
495 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500496 return result.error;
497 }
498
Jamie Madillccdf74b2015-08-18 10:46:12 -0400499 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400500 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400501
Geoff Lang7dd2e102014-11-10 15:19:26 -0500502 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400503 return gl::Error(GL_NO_ERROR);
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000504}
505
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000506// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507void Program::unlink(bool destroy)
508{
509 if (destroy) // Object being destructed
510 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400511 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400513 mData.mAttachedFragmentShader->release();
514 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515 }
516
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400517 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400519 mData.mAttachedVertexShader->release();
520 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522 }
523
Jamie Madillc349ec02015-08-21 16:53:12 -0400524 mData.mAttributes.clear();
Jamie Madill63805b42015-08-25 13:17:39 -0400525 mData.mActiveAttribLocationsMask.reset();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400526 mData.mTransformFeedbackVaryingVars.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400527 mData.mUniforms.clear();
528 mData.mUniformLocations.clear();
529 mData.mUniformBlocks.clear();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400530 mData.mOutputVariables.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500531
Geoff Lang7dd2e102014-11-10 15:19:26 -0500532 mValidated = false;
533
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000534 mLinked = false;
535}
536
Geoff Lange1a27752015-10-05 13:16:04 -0400537bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000538{
539 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540}
541
Geoff Lang7dd2e102014-11-10 15:19:26 -0500542Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000543{
544 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000545
Geoff Lang7dd2e102014-11-10 15:19:26 -0500546#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
547 return Error(GL_NO_ERROR);
548#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400549 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
550 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000551 {
Jamie Madillf6113162015-05-07 11:49:21 -0400552 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500553 return Error(GL_NO_ERROR);
554 }
555
Geoff Langc46cc2f2015-10-01 17:16:20 -0400556 BinaryInputStream stream(binary, length);
557
Geoff Lang7dd2e102014-11-10 15:19:26 -0500558 int majorVersion = stream.readInt<int>();
559 int minorVersion = stream.readInt<int>();
560 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
561 {
Jamie Madillf6113162015-05-07 11:49:21 -0400562 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500563 return Error(GL_NO_ERROR);
564 }
565
566 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
567 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
568 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
569 {
Jamie Madillf6113162015-05-07 11:49:21 -0400570 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500571 return Error(GL_NO_ERROR);
572 }
573
Jamie Madill63805b42015-08-25 13:17:39 -0400574 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
575 "Too many vertex attribs for mask");
576 mData.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500577
Jamie Madill3da79b72015-04-27 11:09:17 -0400578 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400579 ASSERT(mData.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400580 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
581 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400582 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400583 LoadShaderVar(&stream, &attrib);
584 attrib.location = stream.readInt<int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400585 mData.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400586 }
587
Jamie Madill62d31cb2015-09-11 13:25:51 -0400588 unsigned int uniformCount = stream.readInt<unsigned int>();
589 ASSERT(mData.mUniforms.empty());
590 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
591 {
592 LinkedUniform uniform;
593 LoadShaderVar(&stream, &uniform);
594
595 uniform.blockIndex = stream.readInt<int>();
596 uniform.blockInfo.offset = stream.readInt<int>();
597 uniform.blockInfo.arrayStride = stream.readInt<int>();
598 uniform.blockInfo.matrixStride = stream.readInt<int>();
599 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
600
601 mData.mUniforms.push_back(uniform);
602 }
603
604 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
605 ASSERT(mData.mUniformLocations.empty());
606 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
607 uniformIndexIndex++)
608 {
609 VariableLocation variable;
610 stream.readString(&variable.name);
611 stream.readInt(&variable.element);
612 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400613 stream.readBool(&variable.used);
614 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400615
616 mData.mUniformLocations.push_back(variable);
617 }
618
619 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
620 ASSERT(mData.mUniformBlocks.empty());
621 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
622 ++uniformBlockIndex)
623 {
624 UniformBlock uniformBlock;
625 stream.readString(&uniformBlock.name);
626 stream.readBool(&uniformBlock.isArray);
627 stream.readInt(&uniformBlock.arrayElement);
628 stream.readInt(&uniformBlock.dataSize);
629 stream.readBool(&uniformBlock.vertexStaticUse);
630 stream.readBool(&uniformBlock.fragmentStaticUse);
631
632 unsigned int numMembers = stream.readInt<unsigned int>();
633 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
634 {
635 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
636 }
637
Jamie Madill62d31cb2015-09-11 13:25:51 -0400638 mData.mUniformBlocks.push_back(uniformBlock);
639 }
640
Brandon Jones1048ea72015-10-06 15:34:52 -0700641 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
642 ASSERT(mData.mTransformFeedbackVaryingVars.empty());
643 for (unsigned int transformFeedbackVaryingIndex = 0;
644 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
645 ++transformFeedbackVaryingIndex)
646 {
647 sh::Varying varying;
648 stream.readInt(&varying.arraySize);
649 stream.readInt(&varying.type);
650 stream.readString(&varying.name);
651
652 mData.mTransformFeedbackVaryingVars.push_back(varying);
653 }
654
Jamie Madillada9ecc2015-08-17 12:53:37 -0400655 stream.readInt(&mData.mTransformFeedbackBufferMode);
656
Jamie Madill80a6fc02015-08-21 16:53:16 -0400657 unsigned int outputVarCount = stream.readInt<unsigned int>();
658 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
659 {
660 int locationIndex = stream.readInt<int>();
661 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400662 stream.readInt(&locationData.element);
663 stream.readInt(&locationData.index);
664 stream.readString(&locationData.name);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400665 mData.mOutputVariables[locationIndex] = locationData;
666 }
667
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400668 stream.readInt(&mSamplerUniformRange.start);
669 stream.readInt(&mSamplerUniformRange.end);
670
Geoff Lang7dd2e102014-11-10 15:19:26 -0500671 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
672 if (result.error.isError() || !result.linkSuccess)
673 {
Geoff Langb543aff2014-09-30 14:52:54 -0400674 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000675 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000676
Geoff Lang7dd2e102014-11-10 15:19:26 -0500677 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400678 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500679#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
680}
681
682Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
683{
684 if (binaryFormat)
685 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400686 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500687 }
688
689 BinaryOutputStream stream;
690
Geoff Lang7dd2e102014-11-10 15:19:26 -0500691 stream.writeInt(ANGLE_MAJOR_VERSION);
692 stream.writeInt(ANGLE_MINOR_VERSION);
693 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
694
Jamie Madill63805b42015-08-25 13:17:39 -0400695 stream.writeInt(mData.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500696
Jamie Madillc349ec02015-08-21 16:53:12 -0400697 stream.writeInt(mData.mAttributes.size());
698 for (const sh::Attribute &attrib : mData.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400699 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400701 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400702 }
703
704 stream.writeInt(mData.mUniforms.size());
705 for (const gl::LinkedUniform &uniform : mData.mUniforms)
706 {
707 WriteShaderVar(&stream, uniform);
708
709 // FIXME: referenced
710
711 stream.writeInt(uniform.blockIndex);
712 stream.writeInt(uniform.blockInfo.offset);
713 stream.writeInt(uniform.blockInfo.arrayStride);
714 stream.writeInt(uniform.blockInfo.matrixStride);
715 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
716 }
717
718 stream.writeInt(mData.mUniformLocations.size());
719 for (const auto &variable : mData.mUniformLocations)
720 {
721 stream.writeString(variable.name);
722 stream.writeInt(variable.element);
723 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400724 stream.writeInt(variable.used);
725 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400726 }
727
728 stream.writeInt(mData.mUniformBlocks.size());
729 for (const UniformBlock &uniformBlock : mData.mUniformBlocks)
730 {
731 stream.writeString(uniformBlock.name);
732 stream.writeInt(uniformBlock.isArray);
733 stream.writeInt(uniformBlock.arrayElement);
734 stream.writeInt(uniformBlock.dataSize);
735
736 stream.writeInt(uniformBlock.vertexStaticUse);
737 stream.writeInt(uniformBlock.fragmentStaticUse);
738
739 stream.writeInt(uniformBlock.memberUniformIndexes.size());
740 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
741 {
742 stream.writeInt(memberUniformIndex);
743 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400744 }
745
Brandon Jones1048ea72015-10-06 15:34:52 -0700746 stream.writeInt(mData.mTransformFeedbackVaryingVars.size());
747 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
748 {
749 stream.writeInt(varying.arraySize);
750 stream.writeInt(varying.type);
751 stream.writeString(varying.name);
752 }
753
Jamie Madillada9ecc2015-08-17 12:53:37 -0400754 stream.writeInt(mData.mTransformFeedbackBufferMode);
755
Jamie Madill80a6fc02015-08-21 16:53:16 -0400756 stream.writeInt(mData.mOutputVariables.size());
757 for (const auto &outputPair : mData.mOutputVariables)
758 {
759 stream.writeInt(outputPair.first);
760 stream.writeInt(outputPair.second.element);
761 stream.writeInt(outputPair.second.index);
762 stream.writeString(outputPair.second.name);
763 }
764
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400765 stream.writeInt(mSamplerUniformRange.start);
766 stream.writeInt(mSamplerUniformRange.end);
767
Geoff Lang7dd2e102014-11-10 15:19:26 -0500768 gl::Error error = mProgram->save(&stream);
769 if (error.isError())
770 {
771 return error;
772 }
773
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700774 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500775 const void *streamData = stream.data();
776
777 if (streamLength > bufSize)
778 {
779 if (length)
780 {
781 *length = 0;
782 }
783
784 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
785 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
786 // sizes and then copy it.
787 return Error(GL_INVALID_OPERATION);
788 }
789
790 if (binary)
791 {
792 char *ptr = reinterpret_cast<char*>(binary);
793
794 memcpy(ptr, streamData, streamLength);
795 ptr += streamLength;
796
797 ASSERT(ptr - streamLength == binary);
798 }
799
800 if (length)
801 {
802 *length = streamLength;
803 }
804
805 return Error(GL_NO_ERROR);
806}
807
808GLint Program::getBinaryLength() const
809{
810 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400811 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500812 if (error.isError())
813 {
814 return 0;
815 }
816
817 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000818}
819
Geoff Langc5629752015-12-07 16:29:04 -0500820void Program::setBinaryRetrievableHint(bool retrievable)
821{
822 // TODO(jmadill) : replace with dirty bits
823 mProgram->setBinaryRetrievableHint(retrievable);
824 mData.mBinaryRetrieveableHint = retrievable;
825}
826
827bool Program::getBinaryRetrievableHint() const
828{
829 return mData.mBinaryRetrieveableHint;
830}
831
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000832void Program::release()
833{
834 mRefCount--;
835
836 if (mRefCount == 0 && mDeleteStatus)
837 {
838 mResourceManager->deleteProgram(mHandle);
839 }
840}
841
842void Program::addRef()
843{
844 mRefCount++;
845}
846
847unsigned int Program::getRefCount() const
848{
849 return mRefCount;
850}
851
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000852int Program::getInfoLogLength() const
853{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400854 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000855}
856
Geoff Lange1a27752015-10-05 13:16:04 -0400857void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000858{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000859 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000860}
861
Geoff Lange1a27752015-10-05 13:16:04 -0400862void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000863{
864 int total = 0;
865
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400866 if (mData.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000867 {
868 if (total < maxCount)
869 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400870 shaders[total] = mData.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200871 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000872 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000873 }
874
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400875 if (mData.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000876 {
877 if (total < maxCount)
878 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400879 shaders[total] = mData.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200880 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000881 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000882 }
883
884 if (count)
885 {
886 *count = total;
887 }
888}
889
Geoff Lange1a27752015-10-05 13:16:04 -0400890GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500891{
Jamie Madillc349ec02015-08-21 16:53:12 -0400892 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500893 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400894 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500895 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400896 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500897 }
898 }
899
Austin Kinrossb8af7232015-03-16 22:33:25 -0700900 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500901}
902
Jamie Madill63805b42015-08-25 13:17:39 -0400903bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400904{
Olli Etuaho401d9fe2015-08-26 10:19:59 +0300905 ASSERT(attribLocation < mData.mActiveAttribLocationsMask.size());
Jamie Madill63805b42015-08-25 13:17:39 -0400906 return mData.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500907}
908
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000909void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
910{
Jamie Madillc349ec02015-08-21 16:53:12 -0400911 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000912 {
913 if (bufsize > 0)
914 {
915 name[0] = '\0';
916 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500917
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000918 if (length)
919 {
920 *length = 0;
921 }
922
923 *type = GL_NONE;
924 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400925 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000926 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400927
928 size_t attributeIndex = 0;
929
930 for (const sh::Attribute &attribute : mData.mAttributes)
931 {
932 // Skip over inactive attributes
933 if (attribute.staticUse)
934 {
935 if (static_cast<size_t>(index) == attributeIndex)
936 {
937 break;
938 }
939 attributeIndex++;
940 }
941 }
942
943 ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
944 const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
945
946 if (bufsize > 0)
947 {
948 const char *string = attrib.name.c_str();
949
950 strncpy(name, string, bufsize);
951 name[bufsize - 1] = '\0';
952
953 if (length)
954 {
955 *length = static_cast<GLsizei>(strlen(name));
956 }
957 }
958
959 // Always a single 'type' instance
960 *size = 1;
961 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000962}
963
Geoff Lange1a27752015-10-05 13:16:04 -0400964GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000965{
Jamie Madillc349ec02015-08-21 16:53:12 -0400966 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400967 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400968 return 0;
969 }
970
971 GLint count = 0;
972
973 for (const sh::Attribute &attrib : mData.mAttributes)
974 {
975 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000976 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500977
978 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000979}
980
Geoff Lange1a27752015-10-05 13:16:04 -0400981GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000982{
Jamie Madillc349ec02015-08-21 16:53:12 -0400983 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400984 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400985 return 0;
986 }
987
988 size_t maxLength = 0;
989
990 for (const sh::Attribute &attrib : mData.mAttributes)
991 {
992 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500993 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400994 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500995 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000996 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500997
Jamie Madillc349ec02015-08-21 16:53:12 -0400998 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500999}
1000
Geoff Lang7dd2e102014-11-10 15:19:26 -05001001GLint Program::getFragDataLocation(const std::string &name) const
1002{
1003 std::string baseName(name);
1004 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001005 for (auto outputPair : mData.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001006 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001007 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001008 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1009 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001010 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001011 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001012 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001013 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001014}
1015
Geoff Lange1a27752015-10-05 13:16:04 -04001016void Program::getActiveUniform(GLuint index,
1017 GLsizei bufsize,
1018 GLsizei *length,
1019 GLint *size,
1020 GLenum *type,
1021 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001022{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001023 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001024 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001025 // index must be smaller than getActiveUniformCount()
1026 ASSERT(index < mData.mUniforms.size());
1027 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001028
1029 if (bufsize > 0)
1030 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001031 std::string string = uniform.name;
1032 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001033 {
1034 string += "[0]";
1035 }
1036
1037 strncpy(name, string.c_str(), bufsize);
1038 name[bufsize - 1] = '\0';
1039
1040 if (length)
1041 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001042 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001043 }
1044 }
1045
Jamie Madill62d31cb2015-09-11 13:25:51 -04001046 *size = uniform.elementCount();
1047 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001048 }
1049 else
1050 {
1051 if (bufsize > 0)
1052 {
1053 name[0] = '\0';
1054 }
1055
1056 if (length)
1057 {
1058 *length = 0;
1059 }
1060
1061 *size = 0;
1062 *type = GL_NONE;
1063 }
1064}
1065
Geoff Lange1a27752015-10-05 13:16:04 -04001066GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001067{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001068 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001069 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001070 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001071 }
1072 else
1073 {
1074 return 0;
1075 }
1076}
1077
Geoff Lange1a27752015-10-05 13:16:04 -04001078GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001079{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001080 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081
1082 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001083 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001084 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001085 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001086 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001087 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001088 size_t length = uniform.name.length() + 1u;
1089 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090 {
1091 length += 3; // Counting in "[0]".
1092 }
1093 maxLength = std::max(length, maxLength);
1094 }
1095 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001096 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097
Jamie Madill62d31cb2015-09-11 13:25:51 -04001098 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099}
1100
1101GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1102{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001103 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1104 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001105 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001106 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001107 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1108 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1109 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1110 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1111 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1112 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1113 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1114 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1115 default:
1116 UNREACHABLE();
1117 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001118 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001119 return 0;
1120}
1121
1122bool Program::isValidUniformLocation(GLint location) const
1123{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001124 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
Geoff Langd8605522016-04-13 10:19:12 -04001125 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size() &&
1126 mData.mUniformLocations[static_cast<size_t>(location)].used);
1127}
1128
1129bool Program::isIgnoredUniformLocation(GLint location) const
1130{
1131 // Location is ignored if it is -1 or it was bound but non-existant in the shader or optimized
1132 // out
1133 return location == -1 ||
1134 (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size() &&
1135 mData.mUniformLocations[static_cast<size_t>(location)].ignored);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001136}
1137
Jamie Madill62d31cb2015-09-11 13:25:51 -04001138const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001139{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001140 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1141 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142}
1143
Jamie Madill62d31cb2015-09-11 13:25:51 -04001144GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001145{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001146 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147}
1148
Jamie Madill62d31cb2015-09-11 13:25:51 -04001149GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001150{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001151 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001152}
1153
1154void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1155{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001156 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001157 mProgram->setUniform1fv(location, count, v);
1158}
1159
1160void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1161{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001162 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001163 mProgram->setUniform2fv(location, count, v);
1164}
1165
1166void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1167{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001168 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169 mProgram->setUniform3fv(location, count, v);
1170}
1171
1172void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1173{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001174 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001175 mProgram->setUniform4fv(location, count, v);
1176}
1177
1178void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1179{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001180 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001181 mProgram->setUniform1iv(location, count, v);
1182}
1183
1184void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1185{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001186 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001187 mProgram->setUniform2iv(location, count, v);
1188}
1189
1190void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1191{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001192 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001193 mProgram->setUniform3iv(location, count, v);
1194}
1195
1196void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1197{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001198 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001199 mProgram->setUniform4iv(location, count, v);
1200}
1201
1202void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1203{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001204 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001205 mProgram->setUniform1uiv(location, count, v);
1206}
1207
1208void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1209{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001210 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211 mProgram->setUniform2uiv(location, count, v);
1212}
1213
1214void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1215{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001216 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001217 mProgram->setUniform3uiv(location, count, v);
1218}
1219
1220void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1221{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001222 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223 mProgram->setUniform4uiv(location, count, v);
1224}
1225
1226void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1227{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001228 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001229 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1230}
1231
1232void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1233{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001234 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001235 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1236}
1237
1238void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1239{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001240 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001241 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1242}
1243
1244void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1245{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001246 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001247 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1248}
1249
1250void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1251{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001252 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1254}
1255
1256void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1257{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001258 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001259 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1260}
1261
1262void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1263{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001264 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001265 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1266}
1267
1268void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1269{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001270 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1272}
1273
1274void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1275{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001276 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001277 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1278}
1279
Geoff Lange1a27752015-10-05 13:16:04 -04001280void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001281{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001282 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283}
1284
Geoff Lange1a27752015-10-05 13:16:04 -04001285void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001286{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001287 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001288}
1289
Geoff Lange1a27752015-10-05 13:16:04 -04001290void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001291{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001292 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001293}
1294
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001295void Program::flagForDeletion()
1296{
1297 mDeleteStatus = true;
1298}
1299
1300bool Program::isFlaggedForDeletion() const
1301{
1302 return mDeleteStatus;
1303}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001304
Brandon Jones43a53e22014-08-28 16:23:22 -07001305void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001306{
1307 mInfoLog.reset();
1308
Geoff Lang7dd2e102014-11-10 15:19:26 -05001309 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001310 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001311 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001312 }
1313 else
1314 {
Jamie Madillf6113162015-05-07 11:49:21 -04001315 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001316 }
1317}
1318
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1320{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001321 // Skip cache if we're using an infolog, so we get the full error.
1322 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1323 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1324 {
1325 return mCachedValidateSamplersResult.value();
1326 }
1327
1328 if (mTextureUnitTypesCache.empty())
1329 {
1330 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1331 }
1332 else
1333 {
1334 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1335 }
1336
1337 // if any two active samplers in a program are of different types, but refer to the same
1338 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1339 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1340 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1341 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1342 {
1343 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1344 ASSERT(uniform.isSampler());
1345
1346 if (!uniform.staticUse)
1347 continue;
1348
1349 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1350 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1351
1352 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1353 {
1354 GLuint textureUnit = dataPtr[arrayElement];
1355
1356 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1357 {
1358 if (infoLog)
1359 {
1360 (*infoLog) << "Sampler uniform (" << textureUnit
1361 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1362 << caps.maxCombinedTextureImageUnits << ")";
1363 }
1364
1365 mCachedValidateSamplersResult = false;
1366 return false;
1367 }
1368
1369 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1370 {
1371 if (textureType != mTextureUnitTypesCache[textureUnit])
1372 {
1373 if (infoLog)
1374 {
1375 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1376 "image unit ("
1377 << textureUnit << ").";
1378 }
1379
1380 mCachedValidateSamplersResult = false;
1381 return false;
1382 }
1383 }
1384 else
1385 {
1386 mTextureUnitTypesCache[textureUnit] = textureType;
1387 }
1388 }
1389 }
1390
1391 mCachedValidateSamplersResult = true;
1392 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001393}
1394
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001395bool Program::isValidated() const
1396{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397 return mValidated;
1398}
1399
Geoff Lange1a27752015-10-05 13:16:04 -04001400GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001402 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403}
1404
1405void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1406{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001407 ASSERT(uniformBlockIndex <
1408 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409
Jamie Madill62d31cb2015-09-11 13:25:51 -04001410 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001411
1412 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001413 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414 std::string string = uniformBlock.name;
1415
Jamie Madill62d31cb2015-09-11 13:25:51 -04001416 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001418 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001419 }
1420
1421 strncpy(uniformBlockName, string.c_str(), bufSize);
1422 uniformBlockName[bufSize - 1] = '\0';
1423
1424 if (length)
1425 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001426 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001428 }
1429}
1430
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001432{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001433 ASSERT(uniformBlockIndex <
1434 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001435
Jamie Madill62d31cb2015-09-11 13:25:51 -04001436 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437
1438 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001439 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440 case GL_UNIFORM_BLOCK_DATA_SIZE:
1441 *params = static_cast<GLint>(uniformBlock.dataSize);
1442 break;
1443 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001444 *params =
1445 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001446 break;
1447 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1448 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1449 break;
1450 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1451 {
1452 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1453 {
1454 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1455 }
1456 }
1457 break;
1458 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001459 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001460 break;
1461 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001462 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001463 break;
1464 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001465 }
1466}
1467
Geoff Lange1a27752015-10-05 13:16:04 -04001468GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001469{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470 int maxLength = 0;
1471
1472 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001473 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001474 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1476 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001477 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478 if (!uniformBlock.name.empty())
1479 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001480 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481
1482 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001483 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001484
1485 maxLength = std::max(length + arrayLength, maxLength);
1486 }
1487 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001488 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489
1490 return maxLength;
1491}
1492
Geoff Lange1a27752015-10-05 13:16:04 -04001493GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001494{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001495 size_t subscript = GL_INVALID_INDEX;
1496 std::string baseName = gl::ParseUniformName(name, &subscript);
1497
1498 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1499 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1500 {
1501 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1502 if (uniformBlock.name == baseName)
1503 {
1504 const bool arrayElementZero =
1505 (subscript == GL_INVALID_INDEX &&
1506 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1507 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1508 {
1509 return blockIndex;
1510 }
1511 }
1512 }
1513
1514 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001515}
1516
Jamie Madill62d31cb2015-09-11 13:25:51 -04001517const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001518{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001519 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1520 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001521}
1522
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001523void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1524{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001525 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001526 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001527}
1528
1529GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1530{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001531 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001532}
1533
1534void Program::resetUniformBlockBindings()
1535{
1536 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1537 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001538 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001539 }
Geoff Lang5d124a62015-09-15 13:03:27 -04001540 mData.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001541}
1542
Geoff Lang48dcae72014-02-05 16:28:24 -05001543void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1544{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001545 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001546 for (GLsizei i = 0; i < count; i++)
1547 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001548 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001549 }
1550
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001551 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001552}
1553
1554void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1555{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001556 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001557 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001558 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1559 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001560 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1561 if (length)
1562 {
1563 *length = lastNameIdx;
1564 }
1565 if (size)
1566 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001567 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001568 }
1569 if (type)
1570 {
1571 *type = varying.type;
1572 }
1573 if (name)
1574 {
1575 memcpy(name, varying.name.c_str(), lastNameIdx);
1576 name[lastNameIdx] = '\0';
1577 }
1578 }
1579}
1580
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001581GLsizei Program::getTransformFeedbackVaryingCount() const
1582{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001583 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001584 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001585 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001586 }
1587 else
1588 {
1589 return 0;
1590 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001591}
1592
1593GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1594{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001595 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001596 {
1597 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001598 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001599 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001600 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1601 }
1602
1603 return maxSize;
1604 }
1605 else
1606 {
1607 return 0;
1608 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001609}
1610
1611GLenum Program::getTransformFeedbackBufferMode() const
1612{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001613 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001614}
1615
Jamie Madillada9ecc2015-08-17 12:53:37 -04001616// static
1617bool Program::linkVaryings(InfoLog &infoLog,
1618 const Shader *vertexShader,
1619 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620{
Jamie Madill4cff2472015-08-21 16:53:18 -04001621 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1622 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001623
Jamie Madill4cff2472015-08-21 16:53:18 -04001624 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001626 bool matched = false;
1627
1628 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001629 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001630 {
1631 continue;
1632 }
1633
Jamie Madill4cff2472015-08-21 16:53:18 -04001634 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001635 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001636 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001637 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001638 ASSERT(!input.isBuiltIn());
1639 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001640 {
1641 return false;
1642 }
1643
Geoff Lang7dd2e102014-11-10 15:19:26 -05001644 matched = true;
1645 break;
1646 }
1647 }
1648
1649 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001650 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001651 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001652 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001653 return false;
1654 }
1655 }
1656
Jamie Madillada9ecc2015-08-17 12:53:37 -04001657 // TODO(jmadill): verify no unmatched vertex varyings?
1658
Geoff Lang7dd2e102014-11-10 15:19:26 -05001659 return true;
1660}
1661
Geoff Langd8605522016-04-13 10:19:12 -04001662bool Program::linkUniforms(gl::InfoLog &infoLog,
1663 const gl::Caps &caps,
1664 const Bindings &uniformBindings)
Jamie Madillea918db2015-08-18 14:48:59 -04001665{
1666 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1667 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1668
1669 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001670 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001671
1672 for (const sh::Uniform &vertexUniform : vertexUniforms)
1673 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001674 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001675 }
1676
1677 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1678 {
1679 auto entry = linkedUniforms.find(fragmentUniform.name);
1680 if (entry != linkedUniforms.end())
1681 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001682 LinkedUniform *vertexUniform = &entry->second;
1683 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1684 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001685 {
1686 return false;
1687 }
1688 }
1689 }
1690
Jamie Madill62d31cb2015-09-11 13:25:51 -04001691 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1692 // Also check the maximum uniform vector and sampler counts.
1693 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1694 {
1695 return false;
1696 }
1697
Geoff Langd8605522016-04-13 10:19:12 -04001698 if (!indexUniforms(infoLog, caps, uniformBindings))
1699 {
1700 return false;
1701 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001702
Jamie Madillea918db2015-08-18 14:48:59 -04001703 return true;
1704}
1705
Geoff Langd8605522016-04-13 10:19:12 -04001706bool Program::indexUniforms(gl::InfoLog &infoLog,
1707 const gl::Caps &caps,
1708 const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001709{
Geoff Langd8605522016-04-13 10:19:12 -04001710 // Uniforms awaiting a location
1711 std::vector<VariableLocation> unboundUniforms;
1712 std::map<GLuint, VariableLocation> boundUniforms;
1713 int maxUniformLocation = -1;
1714
1715 // Gather bound and unbound uniforms
Jamie Madill62d31cb2015-09-11 13:25:51 -04001716 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1717 {
1718 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1719
Geoff Langd8605522016-04-13 10:19:12 -04001720 if (uniform.isBuiltIn())
1721 {
1722 continue;
1723 }
1724
1725 int bindingLocation = uniformBindings.getBinding(uniform.name);
1726
1727 // Verify that this location isn't bound twice
1728 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
1729 {
1730 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
1731 return false;
1732 }
1733
Jamie Madill62d31cb2015-09-11 13:25:51 -04001734 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1735 {
Geoff Langd8605522016-04-13 10:19:12 -04001736 VariableLocation location(uniform.name, arrayIndex,
1737 static_cast<unsigned int>(uniformIndex));
1738
1739 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001740 {
Geoff Langd8605522016-04-13 10:19:12 -04001741 boundUniforms[bindingLocation] = location;
1742 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
1743 }
1744 else
1745 {
1746 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001747 }
1748 }
1749 }
Geoff Langd8605522016-04-13 10:19:12 -04001750
1751 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
1752 // not be assigned to those locations.
1753 std::set<GLuint> reservedLocations;
1754 for (const auto &binding : uniformBindings)
1755 {
1756 GLuint location = binding.second;
1757 if (boundUniforms.find(location) == boundUniforms.end())
1758 {
1759 reservedLocations.insert(location);
1760 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
1761 }
1762 }
1763
1764 // Make enough space for all uniforms, bound and unbound
1765 mData.mUniformLocations.resize(
1766 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
1767 static_cast<size_t>(maxUniformLocation + 1)));
1768
1769 // Assign bound uniforms
1770 for (const auto &boundUniform : boundUniforms)
1771 {
1772 mData.mUniformLocations[boundUniform.first] = boundUniform.second;
1773 }
1774
1775 // Assign reserved uniforms
1776 for (const auto &reservedLocation : reservedLocations)
1777 {
1778 mData.mUniformLocations[reservedLocation].ignored = true;
1779 }
1780
1781 // Assign unbound uniforms
1782 size_t nextUniformLocation = 0;
1783 for (const auto &unboundUniform : unboundUniforms)
1784 {
1785 while (mData.mUniformLocations[nextUniformLocation].used ||
1786 mData.mUniformLocations[nextUniformLocation].ignored)
1787 {
1788 nextUniformLocation++;
1789 }
1790
1791 ASSERT(nextUniformLocation < mData.mUniformLocations.size());
1792 mData.mUniformLocations[nextUniformLocation] = unboundUniform;
1793 nextUniformLocation++;
1794 }
1795
1796 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001797}
1798
Geoff Lang7dd2e102014-11-10 15:19:26 -05001799bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1800{
Jamie Madillc4c744222015-11-04 09:39:47 -05001801 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1802 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803 {
1804 return false;
1805 }
1806
1807 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1808 {
Jamie Madillf6113162015-05-07 11:49:21 -04001809 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001810 return false;
1811 }
1812
1813 return true;
1814}
1815
1816// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001817bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001818 InfoLog &infoLog,
Geoff Langd8605522016-04-13 10:19:12 -04001819 const Bindings &attributeBindings,
Jamie Madill3da79b72015-04-27 11:09:17 -04001820 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001822 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001823 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001824 GLuint maxAttribs = data.caps->maxVertexAttributes;
1825
1826 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001827 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001828 {
Jamie Madillf6113162015-05-07 11:49:21 -04001829 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001830 return false;
1831 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832
Jamie Madillc349ec02015-08-21 16:53:12 -04001833 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001834
Jamie Madillc349ec02015-08-21 16:53:12 -04001835 // Link attributes that have a binding location
1836 for (sh::Attribute &attribute : mData.mAttributes)
1837 {
1838 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001839 ASSERT(attribute.staticUse);
1840
Geoff Langd8605522016-04-13 10:19:12 -04001841 int bindingLocation = attributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001842 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001843 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001844 attribute.location = bindingLocation;
1845 }
1846
1847 if (attribute.location != -1)
1848 {
1849 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001850 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851
Jamie Madill63805b42015-08-25 13:17:39 -04001852 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001853 {
Jamie Madillf6113162015-05-07 11:49:21 -04001854 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001855 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001856
1857 return false;
1858 }
1859
Jamie Madill63805b42015-08-25 13:17:39 -04001860 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861 {
Jamie Madill63805b42015-08-25 13:17:39 -04001862 const int regLocation = attribute.location + reg;
1863 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001864
1865 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001866 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001867 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001869 // TODO(jmadill): fix aliasing on ES2
1870 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001872 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001873 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 return false;
1875 }
1876 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001877 else
1878 {
Jamie Madill63805b42015-08-25 13:17:39 -04001879 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001880 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001881
Jamie Madill63805b42015-08-25 13:17:39 -04001882 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001883 }
1884 }
1885 }
1886
1887 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001888 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001889 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001890 ASSERT(attribute.staticUse);
1891
Jamie Madillc349ec02015-08-21 16:53:12 -04001892 // Not set by glBindAttribLocation or by location layout qualifier
1893 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001894 {
Jamie Madill63805b42015-08-25 13:17:39 -04001895 int regs = VariableRegisterCount(attribute.type);
1896 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001897
Jamie Madill63805b42015-08-25 13:17:39 -04001898 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001899 {
Jamie Madillf6113162015-05-07 11:49:21 -04001900 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001901 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001902 }
1903
Jamie Madillc349ec02015-08-21 16:53:12 -04001904 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001905 }
1906 }
1907
Jamie Madillc349ec02015-08-21 16:53:12 -04001908 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001909 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001910 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001911 ASSERT(attribute.location != -1);
1912 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001913
Jamie Madill63805b42015-08-25 13:17:39 -04001914 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001915 {
Jamie Madill63805b42015-08-25 13:17:39 -04001916 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001917 }
1918 }
1919
Geoff Lang7dd2e102014-11-10 15:19:26 -05001920 return true;
1921}
1922
Jamie Madille473dee2015-08-18 14:49:01 -04001923bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001924{
Jamie Madille473dee2015-08-18 14:49:01 -04001925 const Shader &vertexShader = *mData.mAttachedVertexShader;
1926 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1927
Geoff Lang7dd2e102014-11-10 15:19:26 -05001928 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1929 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001930
Geoff Lang7dd2e102014-11-10 15:19:26 -05001931 // Check that interface blocks defined in the vertex and fragment shaders are identical
1932 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1933 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001934
1935 GLuint vertexBlockCount = 0;
1936 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001937 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001938 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001939
1940 // Note: shared and std140 layouts are always considered active
1941 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1942 {
1943 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1944 {
1945 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1946 << caps.maxVertexUniformBlocks << ")";
1947 return false;
1948 }
1949 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001950 }
Jamie Madille473dee2015-08-18 14:49:01 -04001951
1952 GLuint fragmentBlockCount = 0;
1953 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 {
Jamie Madille473dee2015-08-18 14:49:01 -04001955 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001956 if (entry != linkedUniformBlocks.end())
1957 {
1958 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1959 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1960 {
1961 return false;
1962 }
1963 }
Jamie Madille473dee2015-08-18 14:49:01 -04001964
Geoff Lang7dd2e102014-11-10 15:19:26 -05001965 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001966 if (fragmentInterfaceBlock.staticUse ||
1967 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968 {
Jamie Madille473dee2015-08-18 14:49:01 -04001969 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970 {
Jamie Madille473dee2015-08-18 14:49:01 -04001971 infoLog
1972 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1973 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001974 return false;
1975 }
1976 }
1977 }
Jamie Madille473dee2015-08-18 14:49:01 -04001978
Geoff Lang7dd2e102014-11-10 15:19:26 -05001979 return true;
1980}
1981
1982bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1983 const sh::InterfaceBlock &fragmentInterfaceBlock)
1984{
1985 const char* blockName = vertexInterfaceBlock.name.c_str();
1986 // validate blocks for the same member types
1987 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1988 {
Jamie Madillf6113162015-05-07 11:49:21 -04001989 infoLog << "Types for interface block '" << blockName
1990 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001991 return false;
1992 }
1993 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1994 {
Jamie Madillf6113162015-05-07 11:49:21 -04001995 infoLog << "Array sizes differ for interface block '" << blockName
1996 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997 return false;
1998 }
1999 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2000 {
Jamie Madillf6113162015-05-07 11:49:21 -04002001 infoLog << "Layout qualifiers differ for interface block '" << blockName
2002 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003 return false;
2004 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002005 const unsigned int numBlockMembers =
2006 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2008 {
2009 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2010 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2011 if (vertexMember.name != fragmentMember.name)
2012 {
Jamie Madillf6113162015-05-07 11:49:21 -04002013 infoLog << "Name mismatch for field " << blockMemberIndex
2014 << " of interface block '" << blockName
2015 << "': (in vertex: '" << vertexMember.name
2016 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002017 return false;
2018 }
2019 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2020 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2021 {
2022 return false;
2023 }
2024 }
2025 return true;
2026}
2027
2028bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2029 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2030{
2031 if (vertexVariable.type != fragmentVariable.type)
2032 {
Jamie Madillf6113162015-05-07 11:49:21 -04002033 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034 return false;
2035 }
2036 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2037 {
Jamie Madillf6113162015-05-07 11:49:21 -04002038 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039 return false;
2040 }
2041 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2042 {
Jamie Madillf6113162015-05-07 11:49:21 -04002043 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002044 return false;
2045 }
2046
2047 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2048 {
Jamie Madillf6113162015-05-07 11:49:21 -04002049 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 return false;
2051 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002052 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2054 {
2055 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2056 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2057
2058 if (vertexMember.name != fragmentMember.name)
2059 {
Jamie Madillf6113162015-05-07 11:49:21 -04002060 infoLog << "Name mismatch for field '" << memberIndex
2061 << "' of " << variableName
2062 << ": (in vertex: '" << vertexMember.name
2063 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064 return false;
2065 }
2066
2067 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2068 vertexMember.name + "'";
2069
2070 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2071 {
2072 return false;
2073 }
2074 }
2075
2076 return true;
2077}
2078
2079bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2080{
Cooper Partin1acf4382015-06-12 12:38:57 -07002081#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2082 const bool validatePrecision = true;
2083#else
2084 const bool validatePrecision = false;
2085#endif
2086
2087 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002088 {
2089 return false;
2090 }
2091
2092 return true;
2093}
2094
2095bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
2096{
2097 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2098 {
2099 return false;
2100 }
2101
Jamie Madille9cc4692015-02-19 16:00:13 -05002102 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002103 {
Jamie Madillf6113162015-05-07 11:49:21 -04002104 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002105 return false;
2106 }
2107
2108 return true;
2109}
2110
Jamie Madillccdf74b2015-08-18 10:46:12 -04002111bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2112 const std::vector<const sh::Varying *> &varyings,
2113 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002114{
2115 size_t totalComponents = 0;
2116
Jamie Madillccdf74b2015-08-18 10:46:12 -04002117 std::set<std::string> uniqueNames;
2118
2119 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002120 {
2121 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002122 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002124 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002126 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002128 infoLog << "Two transform feedback varyings specify the same output variable ("
2129 << tfVaryingName << ").";
2130 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002131 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002132 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002133
Geoff Lang1a683462015-09-29 15:09:59 -04002134 if (varying->isArray())
2135 {
2136 infoLog << "Capture of arrays is undefined and not supported.";
2137 return false;
2138 }
2139
Jamie Madillccdf74b2015-08-18 10:46:12 -04002140 // TODO(jmadill): Investigate implementation limits on D3D11
2141 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04002142 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002143 componentCount > caps.maxTransformFeedbackSeparateComponents)
2144 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002145 infoLog << "Transform feedback varying's " << varying->name << " components ("
2146 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002147 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148 return false;
2149 }
2150
2151 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152 found = true;
2153 break;
2154 }
2155 }
2156
Jamie Madill89bb70e2015-08-31 14:18:39 -04002157 if (tfVaryingName.find('[') != std::string::npos)
2158 {
Geoff Lang1a683462015-09-29 15:09:59 -04002159 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002160 return false;
2161 }
2162
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2164 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002165 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002166 }
2167
Jamie Madillada9ecc2015-08-17 12:53:37 -04002168 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002169 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002170 {
Jamie Madillf6113162015-05-07 11:49:21 -04002171 infoLog << "Transform feedback varying total components (" << totalComponents
2172 << ") exceed the maximum interleaved components ("
2173 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002174 return false;
2175 }
2176
2177 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002178}
2179
Jamie Madillccdf74b2015-08-18 10:46:12 -04002180void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2181{
2182 // Gather the linked varyings that are used for transform feedback, they should all exist.
2183 mData.mTransformFeedbackVaryingVars.clear();
2184 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2185 {
2186 for (const sh::Varying *varying : varyings)
2187 {
2188 if (tfVaryingName == varying->name)
2189 {
2190 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2191 break;
2192 }
2193 }
2194 }
2195}
2196
2197std::vector<const sh::Varying *> Program::getMergedVaryings() const
2198{
2199 std::set<std::string> uniqueNames;
2200 std::vector<const sh::Varying *> varyings;
2201
2202 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2203 {
2204 if (uniqueNames.count(varying.name) == 0)
2205 {
2206 uniqueNames.insert(varying.name);
2207 varyings.push_back(&varying);
2208 }
2209 }
2210
2211 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2212 {
2213 if (uniqueNames.count(varying.name) == 0)
2214 {
2215 uniqueNames.insert(varying.name);
2216 varyings.push_back(&varying);
2217 }
2218 }
2219
2220 return varyings;
2221}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002222
2223void Program::linkOutputVariables()
2224{
2225 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2226 ASSERT(fragmentShader != nullptr);
2227
2228 // Skip this step for GLES2 shaders.
2229 if (fragmentShader->getShaderVersion() == 100)
2230 return;
2231
Jamie Madilla0a9e122015-09-02 15:54:30 -04002232 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002233
2234 // TODO(jmadill): any caps validation here?
2235
2236 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2237 outputVariableIndex++)
2238 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002239 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002240
2241 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2242 if (outputVariable.isBuiltIn())
2243 continue;
2244
2245 // Since multiple output locations must be specified, use 0 for non-specified locations.
2246 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2247
2248 ASSERT(outputVariable.staticUse);
2249
2250 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2251 elementIndex++)
2252 {
2253 const int location = baseLocation + elementIndex;
2254 ASSERT(mData.mOutputVariables.count(location) == 0);
2255 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2256 mData.mOutputVariables[location] =
2257 VariableLocation(outputVariable.name, element, outputVariableIndex);
2258 }
2259 }
2260}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002261
2262bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2263{
2264 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2265 VectorAndSamplerCount vsCounts;
2266
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002267 std::vector<LinkedUniform> samplerUniforms;
2268
Jamie Madill62d31cb2015-09-11 13:25:51 -04002269 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2270 {
2271 if (uniform.staticUse)
2272 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002273 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002274 }
2275 }
2276
2277 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2278 {
2279 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2280 << caps.maxVertexUniformVectors << ").";
2281 return false;
2282 }
2283
2284 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2285 {
2286 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2287 << caps.maxVertexTextureImageUnits << ").";
2288 return false;
2289 }
2290
2291 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2292 VectorAndSamplerCount fsCounts;
2293
2294 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2295 {
2296 if (uniform.staticUse)
2297 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002298 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002299 }
2300 }
2301
2302 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2303 {
2304 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2305 << caps.maxFragmentUniformVectors << ").";
2306 return false;
2307 }
2308
2309 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2310 {
2311 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2312 << caps.maxTextureImageUnits << ").";
2313 return false;
2314 }
2315
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002316 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2317 mSamplerUniformRange.end =
2318 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2319
2320 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2321
Jamie Madill62d31cb2015-09-11 13:25:51 -04002322 return true;
2323}
2324
2325Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002326 const std::string &fullName,
2327 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002328{
2329 VectorAndSamplerCount vectorAndSamplerCount;
2330
2331 if (uniform.isStruct())
2332 {
2333 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2334 {
2335 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2336
2337 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2338 {
2339 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2340 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2341
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002342 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002343 }
2344 }
2345
2346 return vectorAndSamplerCount;
2347 }
2348
2349 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002350 bool isSampler = IsSamplerType(uniform.type);
2351 if (!UniformInList(mData.getUniforms(), fullName) && !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002352 {
2353 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2354 uniform.arraySize, -1,
2355 sh::BlockMemberInfo::getDefaultBlockInfo());
2356 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002357
2358 // Store sampler uniforms separately, so we'll append them to the end of the list.
2359 if (isSampler)
2360 {
2361 samplerUniforms->push_back(linkedUniform);
2362 }
2363 else
2364 {
2365 mData.mUniforms.push_back(linkedUniform);
2366 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002367 }
2368
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002369 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002370
2371 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2372 // Likewise, don't count "real" uniforms towards sampler count.
2373 vectorAndSamplerCount.vectorCount =
2374 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002375 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002376
2377 return vectorAndSamplerCount;
2378}
2379
2380void Program::gatherInterfaceBlockInfo()
2381{
2382 std::set<std::string> visitedList;
2383
2384 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2385
2386 ASSERT(mData.mUniformBlocks.empty());
2387 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2388 {
2389 // Only 'packed' blocks are allowed to be considered inacive.
2390 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2391 continue;
2392
2393 if (visitedList.count(vertexBlock.name) > 0)
2394 continue;
2395
2396 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2397 visitedList.insert(vertexBlock.name);
2398 }
2399
2400 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2401
2402 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2403 {
2404 // Only 'packed' blocks are allowed to be considered inacive.
2405 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2406 continue;
2407
2408 if (visitedList.count(fragmentBlock.name) > 0)
2409 {
2410 for (gl::UniformBlock &block : mData.mUniformBlocks)
2411 {
2412 if (block.name == fragmentBlock.name)
2413 {
2414 block.fragmentStaticUse = fragmentBlock.staticUse;
2415 }
2416 }
2417
2418 continue;
2419 }
2420
2421 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2422 visitedList.insert(fragmentBlock.name);
2423 }
2424}
2425
Jamie Madill4a3c2342015-10-08 12:58:45 -04002426template <typename VarT>
2427void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2428 const std::string &prefix,
2429 int blockIndex)
2430{
2431 for (const VarT &field : fields)
2432 {
2433 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2434
2435 if (field.isStruct())
2436 {
2437 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2438 {
2439 const std::string uniformElementName =
2440 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2441 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2442 }
2443 }
2444 else
2445 {
2446 // If getBlockMemberInfo returns false, the uniform is optimized out.
2447 sh::BlockMemberInfo memberInfo;
2448 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2449 {
2450 continue;
2451 }
2452
2453 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2454 blockIndex, memberInfo);
2455
2456 // Since block uniforms have no location, we don't need to store them in the uniform
2457 // locations list.
2458 mData.mUniforms.push_back(newUniform);
2459 }
2460 }
2461}
2462
Jamie Madill62d31cb2015-09-11 13:25:51 -04002463void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2464{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002465 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2466 size_t blockSize = 0;
2467
2468 // Don't define this block at all if it's not active in the implementation.
2469 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2470 {
2471 return;
2472 }
2473
2474 // Track the first and last uniform index to determine the range of active uniforms in the
2475 // block.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002476 size_t firstBlockUniformIndex = mData.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002477 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002478 size_t lastBlockUniformIndex = mData.mUniforms.size();
2479
2480 std::vector<unsigned int> blockUniformIndexes;
2481 for (size_t blockUniformIndex = firstBlockUniformIndex;
2482 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2483 {
2484 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2485 }
2486
2487 if (interfaceBlock.arraySize > 0)
2488 {
2489 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2490 {
2491 UniformBlock block(interfaceBlock.name, true, arrayElement);
2492 block.memberUniformIndexes = blockUniformIndexes;
2493
2494 if (shaderType == GL_VERTEX_SHADER)
2495 {
2496 block.vertexStaticUse = interfaceBlock.staticUse;
2497 }
2498 else
2499 {
2500 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2501 block.fragmentStaticUse = interfaceBlock.staticUse;
2502 }
2503
Jamie Madill4a3c2342015-10-08 12:58:45 -04002504 // TODO(jmadill): Determine if we can ever have an inactive array element block.
2505 size_t blockElementSize = 0;
2506 if (!mProgram->getUniformBlockSize(block.nameWithArrayIndex(), &blockElementSize))
2507 {
2508 continue;
2509 }
2510
2511 ASSERT(blockElementSize == blockSize);
2512 block.dataSize = static_cast<unsigned int>(blockElementSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002513 mData.mUniformBlocks.push_back(block);
2514 }
2515 }
2516 else
2517 {
2518 UniformBlock block(interfaceBlock.name, false, 0);
2519 block.memberUniformIndexes = blockUniformIndexes;
2520
2521 if (shaderType == GL_VERTEX_SHADER)
2522 {
2523 block.vertexStaticUse = interfaceBlock.staticUse;
2524 }
2525 else
2526 {
2527 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2528 block.fragmentStaticUse = interfaceBlock.staticUse;
2529 }
2530
Jamie Madill4a3c2342015-10-08 12:58:45 -04002531 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002532 mData.mUniformBlocks.push_back(block);
2533 }
2534}
2535
2536template <typename T>
2537void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2538{
2539 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2540 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2541 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2542
2543 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2544 {
2545 // Do a cast conversion for boolean types. From the spec:
2546 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2547 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2548 for (GLsizei component = 0; component < count; ++component)
2549 {
2550 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2551 }
2552 }
2553 else
2554 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002555 // Invalide the validation cache if we modify the sampler data.
2556 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2557 {
2558 mCachedValidateSamplersResult.reset();
2559 }
2560
Jamie Madill62d31cb2015-09-11 13:25:51 -04002561 memcpy(destPointer, v, sizeof(T) * count);
2562 }
2563}
2564
2565template <size_t cols, size_t rows, typename T>
2566void Program::setMatrixUniformInternal(GLint location,
2567 GLsizei count,
2568 GLboolean transpose,
2569 const T *v)
2570{
2571 if (!transpose)
2572 {
2573 setUniformInternal(location, count * cols * rows, v);
2574 return;
2575 }
2576
2577 // Perform a transposing copy.
2578 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2579 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2580 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2581 for (GLsizei element = 0; element < count; ++element)
2582 {
2583 size_t elementOffset = element * rows * cols;
2584
2585 for (size_t row = 0; row < rows; ++row)
2586 {
2587 for (size_t col = 0; col < cols; ++col)
2588 {
2589 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2590 }
2591 }
2592 }
2593}
2594
2595template <typename DestT>
2596void Program::getUniformInternal(GLint location, DestT *dataOut) const
2597{
2598 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2599 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2600
2601 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2602
2603 GLenum componentType = VariableComponentType(uniform.type);
2604 if (componentType == GLTypeToGLenum<DestT>::value)
2605 {
2606 memcpy(dataOut, srcPointer, uniform.getElementSize());
2607 return;
2608 }
2609
Corentin Wallez6596c462016-03-17 17:26:58 -04002610 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002611
2612 switch (componentType)
2613 {
2614 case GL_INT:
2615 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2616 break;
2617 case GL_UNSIGNED_INT:
2618 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2619 break;
2620 case GL_BOOL:
2621 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2622 break;
2623 case GL_FLOAT:
2624 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2625 break;
2626 default:
2627 UNREACHABLE();
2628 }
2629}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630}