blob: 311bdcfff2de0f7e95b2b2e063588576ee9a58d1 [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 +000029const char * const g_fakepath = "C:\\fakepath";
30
Geoff Lang7dd2e102014-11-10 15:19:26 -050031namespace
32{
33
34unsigned int ParseAndStripArrayIndex(std::string* name)
35{
36 unsigned int subscript = GL_INVALID_INDEX;
37
38 // Strip any trailing array operator and retrieve the subscript
39 size_t open = name->find_last_of('[');
40 size_t close = name->find_last_of(']');
41 if (open != std::string::npos && close == name->length() - 1)
42 {
43 subscript = atoi(name->substr(open + 1).c_str());
44 name->erase(open);
45 }
46
47 return subscript;
48}
49
Jamie Madill62d31cb2015-09-11 13:25:51 -040050void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
51{
52 stream->writeInt(var.type);
53 stream->writeInt(var.precision);
54 stream->writeString(var.name);
55 stream->writeString(var.mappedName);
56 stream->writeInt(var.arraySize);
57 stream->writeInt(var.staticUse);
58 stream->writeString(var.structName);
59 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050060}
61
Jamie Madill62d31cb2015-09-11 13:25:51 -040062void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
63{
64 var->type = stream->readInt<GLenum>();
65 var->precision = stream->readInt<GLenum>();
66 var->name = stream->readString();
67 var->mappedName = stream->readString();
68 var->arraySize = stream->readInt<unsigned int>();
69 var->staticUse = stream->readBool();
70 var->structName = stream->readString();
71}
72
73template <typename VarT>
74void DefineUniformBlockMembers(const std::vector<VarT> &fields,
75 const std::string &prefix,
76 int blockIndex,
77 std::vector<LinkedUniform> *uniformsOut)
78{
79 for (const VarT &field : fields)
80 {
81 const std::string &fieldName = (prefix.empty() ? field.name : prefix + "." + field.name);
82
83 if (field.isStruct())
84 {
85 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
86 {
87 const std::string uniformElementName =
88 fieldName + (field.isArray() ? ArrayString(arrayElement) : "");
89 DefineUniformBlockMembers(field.fields, uniformElementName, blockIndex,
90 uniformsOut);
91 }
92 }
93 else
94 {
95 // TODO(jmadill): record row-majorness?
96 // Block layout is recorded in the Impl.
97 LinkedUniform newUniform(field.type, field.precision, fieldName, field.arraySize,
98 blockIndex, sh::BlockMemberInfo::getDefaultBlockInfo());
99
100 // Since block uniforms have no location, we don't need to store them in the uniform
101 // locations list.
102 uniformsOut->push_back(newUniform);
103 }
104 }
105}
106
107// This simplified cast function doesn't need to worry about advanced concepts like
108// depth range values, or casting to bool.
109template <typename DestT, typename SrcT>
110DestT UniformStateQueryCast(SrcT value);
111
112// From-Float-To-Integer Casts
113template <>
114GLint UniformStateQueryCast(GLfloat value)
115{
116 return clampCast<GLint>(roundf(value));
117}
118
119template <>
120GLuint UniformStateQueryCast(GLfloat value)
121{
122 return clampCast<GLuint>(roundf(value));
123}
124
125// From-Integer-to-Integer Casts
126template <>
127GLint UniformStateQueryCast(GLuint value)
128{
129 return clampCast<GLint>(value);
130}
131
132template <>
133GLuint UniformStateQueryCast(GLint value)
134{
135 return clampCast<GLuint>(value);
136}
137
138// From-Boolean-to-Anything Casts
139template <>
140GLfloat UniformStateQueryCast(GLboolean value)
141{
142 return (value == GL_TRUE ? 1.0f : 0.0f);
143}
144
145template <>
146GLint UniformStateQueryCast(GLboolean value)
147{
148 return (value == GL_TRUE ? 1 : 0);
149}
150
151template <>
152GLuint UniformStateQueryCast(GLboolean value)
153{
154 return (value == GL_TRUE ? 1u : 0u);
155}
156
157// Default to static_cast
158template <typename DestT, typename SrcT>
159DestT UniformStateQueryCast(SrcT value)
160{
161 return static_cast<DestT>(value);
162}
163
164template <typename SrcT, typename DestT>
165void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
166{
167 for (int comp = 0; comp < components; ++comp)
168 {
169 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
170 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
171 size_t offset = comp * 4;
172 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
173 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
174 }
175}
176
177} // anonymous namespace
178
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000179AttributeBindings::AttributeBindings()
180{
181}
182
183AttributeBindings::~AttributeBindings()
184{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000185}
186
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400187InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000188{
189}
190
191InfoLog::~InfoLog()
192{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000193}
194
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400195size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000196{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400197 const std::string &logString = mStream.str();
198 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000199}
200
201void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog)
202{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400203 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000204
205 if (bufSize > 0)
206 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400207 const std::string str(mStream.str());
208
209 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000210 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400211 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
212 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000213 }
214
215 infoLog[index] = '\0';
216 }
217
218 if (length)
219 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400220 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000221 }
222}
223
224// append a santized message to the program info log.
225// The D3D compiler includes a fake file path in some of the warning or error
226// messages, so lets remove all occurrences of this fake file path from the log.
227void InfoLog::appendSanitized(const char *message)
228{
229 std::string msg(message);
230
231 size_t found;
232 do
233 {
234 found = msg.find(g_fakepath);
235 if (found != std::string::npos)
236 {
237 msg.erase(found, strlen(g_fakepath));
238 }
239 }
240 while (found != std::string::npos);
241
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400242 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000243}
244
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000245void InfoLog::reset()
246{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000247}
248
Geoff Lang7dd2e102014-11-10 15:19:26 -0500249VariableLocation::VariableLocation()
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400250 : name(),
251 element(0),
252 index(0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000253{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500254}
255
256VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index)
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400257 : name(name),
258 element(element),
259 index(index)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500260{
261}
262
263LinkedVarying::LinkedVarying()
264{
265}
266
267LinkedVarying::LinkedVarying(const std::string &name, GLenum type, GLsizei size, const std::string &semanticName,
268 unsigned int semanticIndex, unsigned int semanticIndexCount)
269 : name(name), type(type), size(size), semanticName(semanticName), semanticIndex(semanticIndex), semanticIndexCount(semanticIndexCount)
270{
271}
272
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400273Program::Data::Data()
274 : mAttachedFragmentShader(nullptr),
275 mAttachedVertexShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400276 mTransformFeedbackBufferMode(GL_NONE)
277{
278}
279
280Program::Data::~Data()
281{
282 if (mAttachedVertexShader != nullptr)
283 {
284 mAttachedVertexShader->release();
285 }
286
287 if (mAttachedFragmentShader != nullptr)
288 {
289 mAttachedFragmentShader->release();
290 }
291}
292
Jamie Madill62d31cb2015-09-11 13:25:51 -0400293const LinkedUniform *Program::Data::getUniformByName(const std::string &name) const
294{
295 for (const LinkedUniform &linkedUniform : mUniforms)
296 {
297 if (linkedUniform.name == name)
298 {
299 return &linkedUniform;
300 }
301 }
302
303 return nullptr;
304}
305
306GLint Program::Data::getUniformLocation(const std::string &name) const
307{
308 size_t subscript = GL_INVALID_INDEX;
309 std::string baseName = gl::ParseUniformName(name, &subscript);
310
311 for (size_t location = 0; location < mUniformLocations.size(); ++location)
312 {
313 const VariableLocation &uniformLocation = mUniformLocations[location];
314 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
315
316 if (uniform.name == baseName)
317 {
318 if ((uniform.isArray() && uniformLocation.element == subscript) ||
319 (subscript == GL_INVALID_INDEX))
320 {
321 return static_cast<GLint>(location);
322 }
323 }
324 }
325
326 return -1;
327}
328
329GLuint Program::Data::getUniformIndex(const std::string &name) const
330{
331 size_t subscript = GL_INVALID_INDEX;
332 std::string baseName = gl::ParseUniformName(name, &subscript);
333
334 // The app is not allowed to specify array indices other than 0 for arrays of basic types
335 if (subscript != 0 && subscript != GL_INVALID_INDEX)
336 {
337 return GL_INVALID_INDEX;
338 }
339
340 for (size_t index = 0; index < mUniforms.size(); index++)
341 {
342 const LinkedUniform &uniform = mUniforms[index];
343 if (uniform.name == baseName)
344 {
345 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
346 {
347 return static_cast<GLuint>(index);
348 }
349 }
350 }
351
352 return GL_INVALID_INDEX;
353}
354
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400355Program::Program(rx::ImplFactory *factory, ResourceManager *manager, GLuint handle)
356 : mProgram(factory->createProgram(mData)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400357 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500358 mLinked(false),
359 mDeleteStatus(false),
360 mRefCount(0),
361 mResourceManager(manager),
Jamie Madill6fa156b2015-09-22 10:17:01 -0400362 mHandle(handle),
363 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500364{
365 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000366
367 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500368 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369}
370
371Program::~Program()
372{
373 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000374
Geoff Lang7dd2e102014-11-10 15:19:26 -0500375 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376}
377
378bool Program::attachShader(Shader *shader)
379{
380 if (shader->getType() == GL_VERTEX_SHADER)
381 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400382 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383 {
384 return false;
385 }
386
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400387 mData.mAttachedVertexShader = shader;
388 mData.mAttachedVertexShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389 }
390 else if (shader->getType() == GL_FRAGMENT_SHADER)
391 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400392 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393 {
394 return false;
395 }
396
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400397 mData.mAttachedFragmentShader = shader;
398 mData.mAttachedFragmentShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399 }
400 else UNREACHABLE();
401
402 return true;
403}
404
405bool Program::detachShader(Shader *shader)
406{
407 if (shader->getType() == GL_VERTEX_SHADER)
408 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400409 if (mData.mAttachedVertexShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410 {
411 return false;
412 }
413
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400414 shader->release();
415 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416 }
417 else if (shader->getType() == GL_FRAGMENT_SHADER)
418 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400419 if (mData.mAttachedFragmentShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420 {
421 return false;
422 }
423
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400424 shader->release();
425 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426 }
427 else UNREACHABLE();
428
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429 return true;
430}
431
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000432int Program::getAttachedShadersCount() const
433{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400434 return (mData.mAttachedVertexShader ? 1 : 0) + (mData.mAttachedFragmentShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000435}
436
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000437void AttributeBindings::bindAttributeLocation(GLuint index, const char *name)
438{
439 if (index < MAX_VERTEX_ATTRIBS)
440 {
441 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
442 {
443 mAttributeBinding[i].erase(name);
444 }
445
446 mAttributeBinding[index].insert(name);
447 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000448}
449
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450void Program::bindAttributeLocation(GLuint index, const char *name)
451{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000452 mAttributeBindings.bindAttributeLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453}
454
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
456// compiling them into binaries, determining the attribute mappings, and collecting
457// a list of uniforms
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400458Error Program::link(const gl::Data &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000459{
460 unlink(false);
461
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000462 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000463 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000464
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400465 if (!mData.mAttachedFragmentShader || !mData.mAttachedFragmentShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500466 {
467 return Error(GL_NO_ERROR);
468 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400469 ASSERT(mData.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500470
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400471 if (!mData.mAttachedVertexShader || !mData.mAttachedVertexShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500472 {
473 return Error(GL_NO_ERROR);
474 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400475 ASSERT(mData.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500476
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400477 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mData.mAttachedVertexShader))
Jamie Madill437d2662014-12-05 14:23:35 -0500478 {
479 return Error(GL_NO_ERROR);
480 }
481
Jamie Madillada9ecc2015-08-17 12:53:37 -0400482 if (!linkVaryings(mInfoLog, mData.mAttachedVertexShader, mData.mAttachedFragmentShader))
483 {
484 return Error(GL_NO_ERROR);
485 }
486
Jamie Madillea918db2015-08-18 14:48:59 -0400487 if (!linkUniforms(mInfoLog, *data.caps))
488 {
489 return Error(GL_NO_ERROR);
490 }
491
Jamie Madille473dee2015-08-18 14:49:01 -0400492 if (!linkUniformBlocks(mInfoLog, *data.caps))
493 {
494 return Error(GL_NO_ERROR);
495 }
496
Jamie Madillccdf74b2015-08-18 10:46:12 -0400497 const auto &mergedVaryings = getMergedVaryings();
498
499 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, *data.caps))
500 {
501 return Error(GL_NO_ERROR);
502 }
503
Jamie Madill80a6fc02015-08-21 16:53:16 -0400504 linkOutputVariables();
505
Jamie Madillf5f4ad22015-09-02 18:32:38 +0000506 rx::LinkResult result = mProgram->link(data, mInfoLog);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500507 if (result.error.isError() || !result.linkSuccess)
508 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500509 return result.error;
510 }
511
Jamie Madillccdf74b2015-08-18 10:46:12 -0400512 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400513 mProgram->gatherUniformBlockInfo(&mData.mUniformBlocks, &mData.mUniforms);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400514
Geoff Lang7dd2e102014-11-10 15:19:26 -0500515 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400516 return gl::Error(GL_NO_ERROR);
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000517}
518
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000519int AttributeBindings::getAttributeBinding(const std::string &name) const
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000520{
521 for (int location = 0; location < MAX_VERTEX_ATTRIBS; location++)
522 {
523 if (mAttributeBinding[location].find(name) != mAttributeBinding[location].end())
524 {
525 return location;
526 }
527 }
528
529 return -1;
530}
531
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000532// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533void Program::unlink(bool destroy)
534{
535 if (destroy) // Object being destructed
536 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400537 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000538 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400539 mData.mAttachedFragmentShader->release();
540 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541 }
542
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400543 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400545 mData.mAttachedVertexShader->release();
546 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000548 }
549
Jamie Madillc349ec02015-08-21 16:53:12 -0400550 mData.mAttributes.clear();
Jamie Madill63805b42015-08-25 13:17:39 -0400551 mData.mActiveAttribLocationsMask.reset();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400552 mData.mTransformFeedbackVaryingVars.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400553 mData.mUniforms.clear();
554 mData.mUniformLocations.clear();
555 mData.mUniformBlocks.clear();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400556 mData.mOutputVariables.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500557
Geoff Lang7dd2e102014-11-10 15:19:26 -0500558 mValidated = false;
559
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000560 mLinked = false;
561}
562
563bool Program::isLinked()
564{
565 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566}
567
Geoff Lang7dd2e102014-11-10 15:19:26 -0500568Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000569{
570 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000571
Geoff Lang7dd2e102014-11-10 15:19:26 -0500572#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
573 return Error(GL_NO_ERROR);
574#else
575 ASSERT(binaryFormat == mProgram->getBinaryFormat());
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000576
Geoff Lang7dd2e102014-11-10 15:19:26 -0500577 BinaryInputStream stream(binary, length);
578
579 GLenum format = stream.readInt<GLenum>();
580 if (format != mProgram->getBinaryFormat())
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000581 {
Jamie Madillf6113162015-05-07 11:49:21 -0400582 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500583 return Error(GL_NO_ERROR);
584 }
585
586 int majorVersion = stream.readInt<int>();
587 int minorVersion = stream.readInt<int>();
588 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
589 {
Jamie Madillf6113162015-05-07 11:49:21 -0400590 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500591 return Error(GL_NO_ERROR);
592 }
593
594 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
595 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
596 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
597 {
Jamie Madillf6113162015-05-07 11:49:21 -0400598 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500599 return Error(GL_NO_ERROR);
600 }
601
Jamie Madill63805b42015-08-25 13:17:39 -0400602 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
603 "Too many vertex attribs for mask");
604 mData.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500605
Jamie Madill3da79b72015-04-27 11:09:17 -0400606 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400607 ASSERT(mData.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400608 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
609 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400610 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400611 LoadShaderVar(&stream, &attrib);
612 attrib.location = stream.readInt<int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400613 mData.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400614 }
615
Jamie Madill62d31cb2015-09-11 13:25:51 -0400616 unsigned int uniformCount = stream.readInt<unsigned int>();
617 ASSERT(mData.mUniforms.empty());
618 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
619 {
620 LinkedUniform uniform;
621 LoadShaderVar(&stream, &uniform);
622
623 uniform.blockIndex = stream.readInt<int>();
624 uniform.blockInfo.offset = stream.readInt<int>();
625 uniform.blockInfo.arrayStride = stream.readInt<int>();
626 uniform.blockInfo.matrixStride = stream.readInt<int>();
627 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
628
629 mData.mUniforms.push_back(uniform);
630 }
631
632 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
633 ASSERT(mData.mUniformLocations.empty());
634 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
635 uniformIndexIndex++)
636 {
637 VariableLocation variable;
638 stream.readString(&variable.name);
639 stream.readInt(&variable.element);
640 stream.readInt(&variable.index);
641
642 mData.mUniformLocations.push_back(variable);
643 }
644
645 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
646 ASSERT(mData.mUniformBlocks.empty());
647 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
648 ++uniformBlockIndex)
649 {
650 UniformBlock uniformBlock;
651 stream.readString(&uniformBlock.name);
652 stream.readBool(&uniformBlock.isArray);
653 stream.readInt(&uniformBlock.arrayElement);
654 stream.readInt(&uniformBlock.dataSize);
655 stream.readBool(&uniformBlock.vertexStaticUse);
656 stream.readBool(&uniformBlock.fragmentStaticUse);
657
658 unsigned int numMembers = stream.readInt<unsigned int>();
659 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
660 {
661 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
662 }
663
664 // TODO(jmadill): Make D3D-only
665 stream.readInt(&uniformBlock.psRegisterIndex);
666 stream.readInt(&uniformBlock.vsRegisterIndex);
667
668 mData.mUniformBlocks.push_back(uniformBlock);
669 }
670
Jamie Madillada9ecc2015-08-17 12:53:37 -0400671 stream.readInt(&mData.mTransformFeedbackBufferMode);
672
Jamie Madill80a6fc02015-08-21 16:53:16 -0400673 unsigned int outputVarCount = stream.readInt<unsigned int>();
674 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
675 {
676 int locationIndex = stream.readInt<int>();
677 VariableLocation locationData;
Jamie Madill6fa156b2015-09-22 10:17:01 -0400678 stream.readInt(&locationData.element);
679 stream.readInt(&locationData.index);
680 stream.readString(&locationData.name);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400681 mData.mOutputVariables[locationIndex] = locationData;
682 }
683
Jamie Madill6fa156b2015-09-22 10:17:01 -0400684 stream.readInt(&mSamplerUniformRange.start);
685 stream.readInt(&mSamplerUniformRange.end);
686
Geoff Lang7dd2e102014-11-10 15:19:26 -0500687 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
688 if (result.error.isError() || !result.linkSuccess)
689 {
Geoff Langb543aff2014-09-30 14:52:54 -0400690 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000691 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000692
Geoff Lang7dd2e102014-11-10 15:19:26 -0500693 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400694 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500695#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
696}
697
698Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
699{
700 if (binaryFormat)
701 {
702 *binaryFormat = mProgram->getBinaryFormat();
703 }
704
705 BinaryOutputStream stream;
706
707 stream.writeInt(mProgram->getBinaryFormat());
708 stream.writeInt(ANGLE_MAJOR_VERSION);
709 stream.writeInt(ANGLE_MINOR_VERSION);
710 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
711
Jamie Madill63805b42015-08-25 13:17:39 -0400712 stream.writeInt(mData.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500713
Jamie Madillc349ec02015-08-21 16:53:12 -0400714 stream.writeInt(mData.mAttributes.size());
715 for (const sh::Attribute &attrib : mData.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400716 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400717 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400718 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400719 }
720
721 stream.writeInt(mData.mUniforms.size());
722 for (const gl::LinkedUniform &uniform : mData.mUniforms)
723 {
724 WriteShaderVar(&stream, uniform);
725
726 // FIXME: referenced
727
728 stream.writeInt(uniform.blockIndex);
729 stream.writeInt(uniform.blockInfo.offset);
730 stream.writeInt(uniform.blockInfo.arrayStride);
731 stream.writeInt(uniform.blockInfo.matrixStride);
732 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
733 }
734
735 stream.writeInt(mData.mUniformLocations.size());
736 for (const auto &variable : mData.mUniformLocations)
737 {
738 stream.writeString(variable.name);
739 stream.writeInt(variable.element);
740 stream.writeInt(variable.index);
741 }
742
743 stream.writeInt(mData.mUniformBlocks.size());
744 for (const UniformBlock &uniformBlock : mData.mUniformBlocks)
745 {
746 stream.writeString(uniformBlock.name);
747 stream.writeInt(uniformBlock.isArray);
748 stream.writeInt(uniformBlock.arrayElement);
749 stream.writeInt(uniformBlock.dataSize);
750
751 stream.writeInt(uniformBlock.vertexStaticUse);
752 stream.writeInt(uniformBlock.fragmentStaticUse);
753
754 stream.writeInt(uniformBlock.memberUniformIndexes.size());
755 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
756 {
757 stream.writeInt(memberUniformIndex);
758 }
759
760 // TODO(jmadill): make D3D-only
761 stream.writeInt(uniformBlock.psRegisterIndex);
762 stream.writeInt(uniformBlock.vsRegisterIndex);
Jamie Madill3da79b72015-04-27 11:09:17 -0400763 }
764
Jamie Madillada9ecc2015-08-17 12:53:37 -0400765 stream.writeInt(mData.mTransformFeedbackBufferMode);
766
Jamie Madill80a6fc02015-08-21 16:53:16 -0400767 stream.writeInt(mData.mOutputVariables.size());
768 for (const auto &outputPair : mData.mOutputVariables)
769 {
770 stream.writeInt(outputPair.first);
771 stream.writeInt(outputPair.second.element);
772 stream.writeInt(outputPair.second.index);
773 stream.writeString(outputPair.second.name);
774 }
775
Jamie Madill6fa156b2015-09-22 10:17:01 -0400776 stream.writeInt(mSamplerUniformRange.start);
777 stream.writeInt(mSamplerUniformRange.end);
778
Geoff Lang7dd2e102014-11-10 15:19:26 -0500779 gl::Error error = mProgram->save(&stream);
780 if (error.isError())
781 {
782 return error;
783 }
784
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700785 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500786 const void *streamData = stream.data();
787
788 if (streamLength > bufSize)
789 {
790 if (length)
791 {
792 *length = 0;
793 }
794
795 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
796 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
797 // sizes and then copy it.
798 return Error(GL_INVALID_OPERATION);
799 }
800
801 if (binary)
802 {
803 char *ptr = reinterpret_cast<char*>(binary);
804
805 memcpy(ptr, streamData, streamLength);
806 ptr += streamLength;
807
808 ASSERT(ptr - streamLength == binary);
809 }
810
811 if (length)
812 {
813 *length = streamLength;
814 }
815
816 return Error(GL_NO_ERROR);
817}
818
819GLint Program::getBinaryLength() const
820{
821 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400822 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500823 if (error.isError())
824 {
825 return 0;
826 }
827
828 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000829}
830
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000831void Program::release()
832{
833 mRefCount--;
834
835 if (mRefCount == 0 && mDeleteStatus)
836 {
837 mResourceManager->deleteProgram(mHandle);
838 }
839}
840
841void Program::addRef()
842{
843 mRefCount++;
844}
845
846unsigned int Program::getRefCount() const
847{
848 return mRefCount;
849}
850
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000851int Program::getInfoLogLength() const
852{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400853 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000854}
855
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000856void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
857{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000858 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000859}
860
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000861void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders)
862{
863 int total = 0;
864
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400865 if (mData.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000866 {
867 if (total < maxCount)
868 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400869 shaders[total] = mData.mAttachedVertexShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000870 }
871
872 total++;
873 }
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();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000880 }
881
882 total++;
883 }
884
885 if (count)
886 {
887 *count = total;
888 }
889}
890
Geoff Lang7dd2e102014-11-10 15:19:26 -0500891GLuint Program::getAttributeLocation(const std::string &name)
892{
Jamie Madillc349ec02015-08-21 16:53:12 -0400893 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500894 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400895 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500896 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400897 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500898 }
899 }
900
Austin Kinrossb8af7232015-03-16 22:33:25 -0700901 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500902}
903
Jamie Madill63805b42015-08-25 13:17:39 -0400904bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400905{
Olli Etuaho401d9fe2015-08-26 10:19:59 +0300906 ASSERT(attribLocation < mData.mActiveAttribLocationsMask.size());
Jamie Madill63805b42015-08-25 13:17:39 -0400907 return mData.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500908}
909
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000910void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
911{
Jamie Madillc349ec02015-08-21 16:53:12 -0400912 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000913 {
914 if (bufsize > 0)
915 {
916 name[0] = '\0';
917 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500918
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000919 if (length)
920 {
921 *length = 0;
922 }
923
924 *type = GL_NONE;
925 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400926 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000927 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400928
929 size_t attributeIndex = 0;
930
931 for (const sh::Attribute &attribute : mData.mAttributes)
932 {
933 // Skip over inactive attributes
934 if (attribute.staticUse)
935 {
936 if (static_cast<size_t>(index) == attributeIndex)
937 {
938 break;
939 }
940 attributeIndex++;
941 }
942 }
943
944 ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
945 const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
946
947 if (bufsize > 0)
948 {
949 const char *string = attrib.name.c_str();
950
951 strncpy(name, string, bufsize);
952 name[bufsize - 1] = '\0';
953
954 if (length)
955 {
956 *length = static_cast<GLsizei>(strlen(name));
957 }
958 }
959
960 // Always a single 'type' instance
961 *size = 1;
962 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000963}
964
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000965GLint Program::getActiveAttributeCount()
966{
Jamie Madillc349ec02015-08-21 16:53:12 -0400967 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400968 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400969 return 0;
970 }
971
972 GLint count = 0;
973
974 for (const sh::Attribute &attrib : mData.mAttributes)
975 {
976 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000977 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500978
979 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000980}
981
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000982GLint Program::getActiveAttributeMaxLength()
983{
Jamie Madillc349ec02015-08-21 16:53:12 -0400984 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400985 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400986 return 0;
987 }
988
989 size_t maxLength = 0;
990
991 for (const sh::Attribute &attrib : mData.mAttributes)
992 {
993 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500994 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400995 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500996 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000997 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500998
Jamie Madillc349ec02015-08-21 16:53:12 -0400999 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001000}
1001
Geoff Lang7dd2e102014-11-10 15:19:26 -05001002GLint Program::getFragDataLocation(const std::string &name) const
1003{
1004 std::string baseName(name);
1005 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001006 for (auto outputPair : mData.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001007 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001008 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001009 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1010 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001011 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001012 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001013 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001014 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001015}
1016
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001017void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1018{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001019 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001020 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001021 // index must be smaller than getActiveUniformCount()
1022 ASSERT(index < mData.mUniforms.size());
1023 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001024
1025 if (bufsize > 0)
1026 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001027 std::string string = uniform.name;
1028 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001029 {
1030 string += "[0]";
1031 }
1032
1033 strncpy(name, string.c_str(), bufsize);
1034 name[bufsize - 1] = '\0';
1035
1036 if (length)
1037 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001038 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001039 }
1040 }
1041
Jamie Madill62d31cb2015-09-11 13:25:51 -04001042 *size = uniform.elementCount();
1043 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001044 }
1045 else
1046 {
1047 if (bufsize > 0)
1048 {
1049 name[0] = '\0';
1050 }
1051
1052 if (length)
1053 {
1054 *length = 0;
1055 }
1056
1057 *size = 0;
1058 *type = GL_NONE;
1059 }
1060}
1061
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001062GLint Program::getActiveUniformCount()
1063{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001064 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001065 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001066 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001067 }
1068 else
1069 {
1070 return 0;
1071 }
1072}
1073
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001074GLint Program::getActiveUniformMaxLength()
1075{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001076 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001077
1078 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001079 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001080 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001082 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001083 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001084 size_t length = uniform.name.length() + 1u;
1085 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001086 {
1087 length += 3; // Counting in "[0]".
1088 }
1089 maxLength = std::max(length, maxLength);
1090 }
1091 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001092 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001093
Jamie Madill62d31cb2015-09-11 13:25:51 -04001094 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001095}
1096
1097GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1098{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001099 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1100 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001101 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001102 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001103 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1104 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1105 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1106 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1107 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1108 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1109 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1110 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1111 default:
1112 UNREACHABLE();
1113 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001114 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001115 return 0;
1116}
1117
1118bool Program::isValidUniformLocation(GLint location) const
1119{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001120 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
1121 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001122}
1123
Jamie Madill62d31cb2015-09-11 13:25:51 -04001124const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001125{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001126 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1127 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001128}
1129
Jamie Madill62d31cb2015-09-11 13:25:51 -04001130GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001131{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001132 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001133}
1134
Jamie Madill62d31cb2015-09-11 13:25:51 -04001135GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001136{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001137 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001138}
1139
1140void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1141{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001142 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001143 mProgram->setUniform1fv(location, count, v);
1144}
1145
1146void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1147{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001148 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001149 mProgram->setUniform2fv(location, count, v);
1150}
1151
1152void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1153{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001154 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001155 mProgram->setUniform3fv(location, count, v);
1156}
1157
1158void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1159{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001160 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001161 mProgram->setUniform4fv(location, count, v);
1162}
1163
1164void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1165{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001166 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001167 mProgram->setUniform1iv(location, count, v);
1168}
1169
1170void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1171{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001172 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001173 mProgram->setUniform2iv(location, count, v);
1174}
1175
1176void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1177{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001178 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001179 mProgram->setUniform3iv(location, count, v);
1180}
1181
1182void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1183{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001184 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001185 mProgram->setUniform4iv(location, count, v);
1186}
1187
1188void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1189{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001190 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001191 mProgram->setUniform1uiv(location, count, v);
1192}
1193
1194void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1195{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001196 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001197 mProgram->setUniform2uiv(location, count, v);
1198}
1199
1200void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1201{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001202 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001203 mProgram->setUniform3uiv(location, count, v);
1204}
1205
1206void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1207{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001208 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001209 mProgram->setUniform4uiv(location, count, v);
1210}
1211
1212void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1213{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001214 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001215 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1216}
1217
1218void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1219{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001220 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001221 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1222}
1223
1224void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1225{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001226 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001227 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1228}
1229
1230void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1231{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001232 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001233 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1234}
1235
1236void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1237{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001238 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001239 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1240}
1241
1242void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1243{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001244 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001245 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1246}
1247
1248void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1249{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001250 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001251 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1252}
1253
1254void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1255{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001256 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001257 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1258}
1259
1260void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1261{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001262 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001263 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1264}
1265
1266void Program::getUniformfv(GLint location, GLfloat *v)
1267{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001268 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001269}
1270
1271void Program::getUniformiv(GLint location, GLint *v)
1272{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001273 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001274}
1275
1276void Program::getUniformuiv(GLint location, GLuint *v)
1277{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001278 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001279}
1280
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001281void Program::flagForDeletion()
1282{
1283 mDeleteStatus = true;
1284}
1285
1286bool Program::isFlaggedForDeletion() const
1287{
1288 return mDeleteStatus;
1289}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001290
Brandon Jones43a53e22014-08-28 16:23:22 -07001291void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001292{
1293 mInfoLog.reset();
1294
Geoff Lang7dd2e102014-11-10 15:19:26 -05001295 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001296 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001297 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001298 }
1299 else
1300 {
Jamie Madillf6113162015-05-07 11:49:21 -04001301 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001302 }
1303}
1304
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1306{
Jamie Madill6fa156b2015-09-22 10:17:01 -04001307 // Skip cache if we're using an infolog, so we get the full error.
1308 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1309 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1310 {
1311 return mCachedValidateSamplersResult.value();
1312 }
1313
1314 if (mTextureUnitTypesCache.empty())
1315 {
1316 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1317 }
1318 else
1319 {
1320 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1321 }
1322
1323 // if any two active samplers in a program are of different types, but refer to the same
1324 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1325 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1326 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1327 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1328 {
1329 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1330 ASSERT(uniform.isSampler());
1331
1332 if (!uniform.staticUse)
1333 continue;
1334
1335 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1336 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1337
1338 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1339 {
1340 GLuint textureUnit = dataPtr[arrayElement];
1341
1342 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1343 {
1344 if (infoLog)
1345 {
1346 (*infoLog) << "Sampler uniform (" << textureUnit
1347 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1348 << caps.maxCombinedTextureImageUnits << ")";
1349 }
1350
1351 mCachedValidateSamplersResult = false;
1352 return false;
1353 }
1354
1355 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1356 {
1357 if (textureType != mTextureUnitTypesCache[textureUnit])
1358 {
1359 if (infoLog)
1360 {
1361 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1362 "image unit ("
1363 << textureUnit << ").";
1364 }
1365
1366 mCachedValidateSamplersResult = false;
1367 return false;
1368 }
1369 }
1370 else
1371 {
1372 mTextureUnitTypesCache[textureUnit] = textureType;
1373 }
1374 }
1375 }
1376
1377 mCachedValidateSamplersResult = true;
1378 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379}
1380
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001381bool Program::isValidated() const
1382{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383 return mValidated;
1384}
1385
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386GLuint Program::getActiveUniformBlockCount()
1387{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001388 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001389}
1390
1391void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1392{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001393 ASSERT(uniformBlockIndex <
1394 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001395
Jamie Madill62d31cb2015-09-11 13:25:51 -04001396 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397
1398 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001399 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001400 std::string string = uniformBlock.name;
1401
Jamie Madill62d31cb2015-09-11 13:25:51 -04001402 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001404 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405 }
1406
1407 strncpy(uniformBlockName, string.c_str(), bufSize);
1408 uniformBlockName[bufSize - 1] = '\0';
1409
1410 if (length)
1411 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001412 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001414 }
1415}
1416
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001418{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001419 ASSERT(uniformBlockIndex <
1420 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421
Jamie Madill62d31cb2015-09-11 13:25:51 -04001422 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001423
1424 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001425 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426 case GL_UNIFORM_BLOCK_DATA_SIZE:
1427 *params = static_cast<GLint>(uniformBlock.dataSize);
1428 break;
1429 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001430 *params =
1431 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432 break;
1433 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1434 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1435 break;
1436 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1437 {
1438 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1439 {
1440 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1441 }
1442 }
1443 break;
1444 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001445 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001446 break;
1447 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001448 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001449 break;
1450 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001451 }
1452}
1453
1454GLint Program::getActiveUniformBlockMaxLength()
1455{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456 int maxLength = 0;
1457
1458 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001459 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001460 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1462 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001463 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001464 if (!uniformBlock.name.empty())
1465 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001466 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467
1468 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001469 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470
1471 maxLength = std::max(length + arrayLength, maxLength);
1472 }
1473 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001474 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475
1476 return maxLength;
1477}
1478
1479GLuint Program::getUniformBlockIndex(const std::string &name)
1480{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001481 size_t subscript = GL_INVALID_INDEX;
1482 std::string baseName = gl::ParseUniformName(name, &subscript);
1483
1484 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1485 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1486 {
1487 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1488 if (uniformBlock.name == baseName)
1489 {
1490 const bool arrayElementZero =
1491 (subscript == GL_INVALID_INDEX &&
1492 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1493 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1494 {
1495 return blockIndex;
1496 }
1497 }
1498 }
1499
1500 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001501}
1502
Jamie Madill62d31cb2015-09-11 13:25:51 -04001503const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001504{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001505 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1506 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001507}
1508
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001509void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1510{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001511 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001512}
1513
1514GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1515{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001516 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001517}
1518
1519void Program::resetUniformBlockBindings()
1520{
1521 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1522 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001523 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001524 }
1525}
1526
Geoff Lang48dcae72014-02-05 16:28:24 -05001527void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1528{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001529 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001530 for (GLsizei i = 0; i < count; i++)
1531 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001532 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001533 }
1534
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001535 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001536}
1537
1538void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1539{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001540 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001541 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001542 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1543 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001544 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1545 if (length)
1546 {
1547 *length = lastNameIdx;
1548 }
1549 if (size)
1550 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001551 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001552 }
1553 if (type)
1554 {
1555 *type = varying.type;
1556 }
1557 if (name)
1558 {
1559 memcpy(name, varying.name.c_str(), lastNameIdx);
1560 name[lastNameIdx] = '\0';
1561 }
1562 }
1563}
1564
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001565GLsizei Program::getTransformFeedbackVaryingCount() const
1566{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001568 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001569 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001570 }
1571 else
1572 {
1573 return 0;
1574 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001575}
1576
1577GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1578{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001579 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001580 {
1581 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001582 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001583 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001584 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1585 }
1586
1587 return maxSize;
1588 }
1589 else
1590 {
1591 return 0;
1592 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001593}
1594
1595GLenum Program::getTransformFeedbackBufferMode() const
1596{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001597 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001598}
1599
Jamie Madillada9ecc2015-08-17 12:53:37 -04001600// static
1601bool Program::linkVaryings(InfoLog &infoLog,
1602 const Shader *vertexShader,
1603 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001604{
Jamie Madill4cff2472015-08-21 16:53:18 -04001605 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1606 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001607
Jamie Madill4cff2472015-08-21 16:53:18 -04001608 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001609 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001610 bool matched = false;
1611
1612 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001613 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001614 {
1615 continue;
1616 }
1617
Jamie Madill4cff2472015-08-21 16:53:18 -04001618 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001619 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001620 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001621 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001622 ASSERT(!input.isBuiltIn());
1623 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624 {
1625 return false;
1626 }
1627
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628 matched = true;
1629 break;
1630 }
1631 }
1632
1633 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001634 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001635 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001636 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001637 return false;
1638 }
1639 }
1640
Jamie Madillada9ecc2015-08-17 12:53:37 -04001641 // TODO(jmadill): verify no unmatched vertex varyings?
1642
Geoff Lang7dd2e102014-11-10 15:19:26 -05001643 return true;
1644}
1645
Jamie Madill62d31cb2015-09-11 13:25:51 -04001646bool Program::linkUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Jamie Madillea918db2015-08-18 14:48:59 -04001647{
1648 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1649 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1650
1651 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001652 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001653
1654 for (const sh::Uniform &vertexUniform : vertexUniforms)
1655 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001656 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001657 }
1658
1659 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1660 {
1661 auto entry = linkedUniforms.find(fragmentUniform.name);
1662 if (entry != linkedUniforms.end())
1663 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001664 LinkedUniform *vertexUniform = &entry->second;
1665 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1666 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001667 {
1668 return false;
1669 }
1670 }
1671 }
1672
Jamie Madill62d31cb2015-09-11 13:25:51 -04001673 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1674 // Also check the maximum uniform vector and sampler counts.
1675 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1676 {
1677 return false;
1678 }
1679
1680 indexUniforms();
1681
Jamie Madillea918db2015-08-18 14:48:59 -04001682 return true;
1683}
1684
Jamie Madill62d31cb2015-09-11 13:25:51 -04001685void Program::indexUniforms()
1686{
1687 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1688 {
1689 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1690
1691 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1692 {
1693 if (!uniform.isBuiltIn())
1694 {
1695 // Assign in-order uniform locations
1696 mData.mUniformLocations.push_back(gl::VariableLocation(
1697 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)));
1698 }
1699 }
1700 }
1701}
1702
Geoff Lang7dd2e102014-11-10 15:19:26 -05001703bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1704{
1705 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
1706 {
1707 return false;
1708 }
1709
1710 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1711 {
Jamie Madillf6113162015-05-07 11:49:21 -04001712 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001713 return false;
1714 }
1715
1716 return true;
1717}
1718
1719// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001720bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001721 InfoLog &infoLog,
1722 const AttributeBindings &attributeBindings,
1723 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001724{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001725 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001726 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001727 GLuint maxAttribs = data.caps->maxVertexAttributes;
1728
1729 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001730 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001731 {
Jamie Madillf6113162015-05-07 11:49:21 -04001732 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001733 return false;
1734 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001735
Jamie Madillc349ec02015-08-21 16:53:12 -04001736 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001737
Jamie Madillc349ec02015-08-21 16:53:12 -04001738 // Link attributes that have a binding location
1739 for (sh::Attribute &attribute : mData.mAttributes)
1740 {
1741 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001742 ASSERT(attribute.staticUse);
1743
Jamie Madillc349ec02015-08-21 16:53:12 -04001744 int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
1745 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001746 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001747 attribute.location = bindingLocation;
1748 }
1749
1750 if (attribute.location != -1)
1751 {
1752 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001753 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001754
Jamie Madill63805b42015-08-25 13:17:39 -04001755 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001756 {
Jamie Madillf6113162015-05-07 11:49:21 -04001757 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001758 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001759
1760 return false;
1761 }
1762
Jamie Madill63805b42015-08-25 13:17:39 -04001763 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001764 {
Jamie Madill63805b42015-08-25 13:17:39 -04001765 const int regLocation = attribute.location + reg;
1766 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001767
1768 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001769 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001770 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001771 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001772 // TODO(jmadill): fix aliasing on ES2
1773 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001774 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001775 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001776 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777 return false;
1778 }
1779 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001780 else
1781 {
Jamie Madill63805b42015-08-25 13:17:39 -04001782 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001783 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001784
Jamie Madill63805b42015-08-25 13:17:39 -04001785 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001786 }
1787 }
1788 }
1789
1790 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001791 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001793 ASSERT(attribute.staticUse);
1794
Jamie Madillc349ec02015-08-21 16:53:12 -04001795 // Not set by glBindAttribLocation or by location layout qualifier
1796 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001797 {
Jamie Madill63805b42015-08-25 13:17:39 -04001798 int regs = VariableRegisterCount(attribute.type);
1799 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001800
Jamie Madill63805b42015-08-25 13:17:39 -04001801 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802 {
Jamie Madillf6113162015-05-07 11:49:21 -04001803 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001804 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001805 }
1806
Jamie Madillc349ec02015-08-21 16:53:12 -04001807 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001808 }
1809 }
1810
Jamie Madillc349ec02015-08-21 16:53:12 -04001811 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001812 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001813 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001814 ASSERT(attribute.location != -1);
1815 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001816
Jamie Madill63805b42015-08-25 13:17:39 -04001817 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001818 {
Jamie Madill63805b42015-08-25 13:17:39 -04001819 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001820 }
1821 }
1822
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823 return true;
1824}
1825
Jamie Madille473dee2015-08-18 14:49:01 -04001826bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827{
Jamie Madille473dee2015-08-18 14:49:01 -04001828 const Shader &vertexShader = *mData.mAttachedVertexShader;
1829 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1830
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1832 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001833
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834 // Check that interface blocks defined in the vertex and fragment shaders are identical
1835 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1836 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001837
1838 GLuint vertexBlockCount = 0;
1839 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001840 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001842
1843 // Note: shared and std140 layouts are always considered active
1844 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1845 {
1846 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1847 {
1848 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1849 << caps.maxVertexUniformBlocks << ")";
1850 return false;
1851 }
1852 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001853 }
Jamie Madille473dee2015-08-18 14:49:01 -04001854
1855 GLuint fragmentBlockCount = 0;
1856 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001857 {
Jamie Madille473dee2015-08-18 14:49:01 -04001858 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001859 if (entry != linkedUniformBlocks.end())
1860 {
1861 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1862 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1863 {
1864 return false;
1865 }
1866 }
Jamie Madille473dee2015-08-18 14:49:01 -04001867
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001869 if (fragmentInterfaceBlock.staticUse ||
1870 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871 {
Jamie Madille473dee2015-08-18 14:49:01 -04001872 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001873 {
Jamie Madille473dee2015-08-18 14:49:01 -04001874 infoLog
1875 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1876 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001877 return false;
1878 }
1879 }
1880 }
Jamie Madille473dee2015-08-18 14:49:01 -04001881
Jamie Madill62d31cb2015-09-11 13:25:51 -04001882 gatherInterfaceBlockInfo();
1883
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884 return true;
1885}
1886
1887bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1888 const sh::InterfaceBlock &fragmentInterfaceBlock)
1889{
1890 const char* blockName = vertexInterfaceBlock.name.c_str();
1891 // validate blocks for the same member types
1892 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1893 {
Jamie Madillf6113162015-05-07 11:49:21 -04001894 infoLog << "Types for interface block '" << blockName
1895 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001896 return false;
1897 }
1898 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1899 {
Jamie Madillf6113162015-05-07 11:49:21 -04001900 infoLog << "Array sizes differ for interface block '" << blockName
1901 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001902 return false;
1903 }
1904 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
1905 {
Jamie Madillf6113162015-05-07 11:49:21 -04001906 infoLog << "Layout qualifiers differ for interface block '" << blockName
1907 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001908 return false;
1909 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001910 const unsigned int numBlockMembers =
1911 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1913 {
1914 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
1915 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
1916 if (vertexMember.name != fragmentMember.name)
1917 {
Jamie Madillf6113162015-05-07 11:49:21 -04001918 infoLog << "Name mismatch for field " << blockMemberIndex
1919 << " of interface block '" << blockName
1920 << "': (in vertex: '" << vertexMember.name
1921 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001922 return false;
1923 }
1924 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
1925 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
1926 {
1927 return false;
1928 }
1929 }
1930 return true;
1931}
1932
1933bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
1934 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
1935{
1936 if (vertexVariable.type != fragmentVariable.type)
1937 {
Jamie Madillf6113162015-05-07 11:49:21 -04001938 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001939 return false;
1940 }
1941 if (vertexVariable.arraySize != fragmentVariable.arraySize)
1942 {
Jamie Madillf6113162015-05-07 11:49:21 -04001943 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001944 return false;
1945 }
1946 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
1947 {
Jamie Madillf6113162015-05-07 11:49:21 -04001948 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001949 return false;
1950 }
1951
1952 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
1953 {
Jamie Madillf6113162015-05-07 11:49:21 -04001954 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001955 return false;
1956 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001957 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001958 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1959 {
1960 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
1961 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
1962
1963 if (vertexMember.name != fragmentMember.name)
1964 {
Jamie Madillf6113162015-05-07 11:49:21 -04001965 infoLog << "Name mismatch for field '" << memberIndex
1966 << "' of " << variableName
1967 << ": (in vertex: '" << vertexMember.name
1968 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001969 return false;
1970 }
1971
1972 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
1973 vertexMember.name + "'";
1974
1975 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
1976 {
1977 return false;
1978 }
1979 }
1980
1981 return true;
1982}
1983
1984bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
1985{
Cooper Partin1acf4382015-06-12 12:38:57 -07001986#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
1987 const bool validatePrecision = true;
1988#else
1989 const bool validatePrecision = false;
1990#endif
1991
1992 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001993 {
1994 return false;
1995 }
1996
1997 return true;
1998}
1999
2000bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
2001{
2002 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2003 {
2004 return false;
2005 }
2006
Jamie Madille9cc4692015-02-19 16:00:13 -05002007 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002008 {
Jamie Madillf6113162015-05-07 11:49:21 -04002009 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002010 return false;
2011 }
2012
2013 return true;
2014}
2015
Jamie Madillccdf74b2015-08-18 10:46:12 -04002016bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2017 const std::vector<const sh::Varying *> &varyings,
2018 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019{
2020 size_t totalComponents = 0;
2021
Jamie Madillccdf74b2015-08-18 10:46:12 -04002022 std::set<std::string> uniqueNames;
2023
2024 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002025 {
2026 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002027 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002028 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002029 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002030 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002031 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002032 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002033 infoLog << "Two transform feedback varyings specify the same output variable ("
2034 << tfVaryingName << ").";
2035 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002037 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002038
Jamie Madillccdf74b2015-08-18 10:46:12 -04002039 // TODO(jmadill): Investigate implementation limits on D3D11
2040 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04002041 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042 componentCount > caps.maxTransformFeedbackSeparateComponents)
2043 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002044 infoLog << "Transform feedback varying's " << varying->name << " components ("
2045 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002046 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002047 return false;
2048 }
2049
2050 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002051 found = true;
2052 break;
2053 }
2054 }
2055
Jamie Madill89bb70e2015-08-31 14:18:39 -04002056 // TODO(jmadill): investigate if we can support capturing array elements.
2057 if (tfVaryingName.find('[') != std::string::npos)
2058 {
2059 infoLog << "Capture of array elements not currently supported.";
2060 return false;
2061 }
2062
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2064 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002065 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002066 }
2067
Jamie Madillada9ecc2015-08-17 12:53:37 -04002068 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002069 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002070 {
Jamie Madillf6113162015-05-07 11:49:21 -04002071 infoLog << "Transform feedback varying total components (" << totalComponents
2072 << ") exceed the maximum interleaved components ("
2073 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074 return false;
2075 }
2076
2077 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002078}
2079
Jamie Madillccdf74b2015-08-18 10:46:12 -04002080void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2081{
2082 // Gather the linked varyings that are used for transform feedback, they should all exist.
2083 mData.mTransformFeedbackVaryingVars.clear();
2084 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2085 {
2086 for (const sh::Varying *varying : varyings)
2087 {
2088 if (tfVaryingName == varying->name)
2089 {
2090 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2091 break;
2092 }
2093 }
2094 }
2095}
2096
2097std::vector<const sh::Varying *> Program::getMergedVaryings() const
2098{
2099 std::set<std::string> uniqueNames;
2100 std::vector<const sh::Varying *> varyings;
2101
2102 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2103 {
2104 if (uniqueNames.count(varying.name) == 0)
2105 {
2106 uniqueNames.insert(varying.name);
2107 varyings.push_back(&varying);
2108 }
2109 }
2110
2111 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2112 {
2113 if (uniqueNames.count(varying.name) == 0)
2114 {
2115 uniqueNames.insert(varying.name);
2116 varyings.push_back(&varying);
2117 }
2118 }
2119
2120 return varyings;
2121}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002122
2123void Program::linkOutputVariables()
2124{
2125 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2126 ASSERT(fragmentShader != nullptr);
2127
2128 // Skip this step for GLES2 shaders.
2129 if (fragmentShader->getShaderVersion() == 100)
2130 return;
2131
Jamie Madilla0a9e122015-09-02 15:54:30 -04002132 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002133
2134 // TODO(jmadill): any caps validation here?
2135
2136 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2137 outputVariableIndex++)
2138 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002139 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002140
2141 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2142 if (outputVariable.isBuiltIn())
2143 continue;
2144
2145 // Since multiple output locations must be specified, use 0 for non-specified locations.
2146 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2147
2148 ASSERT(outputVariable.staticUse);
2149
2150 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2151 elementIndex++)
2152 {
2153 const int location = baseLocation + elementIndex;
2154 ASSERT(mData.mOutputVariables.count(location) == 0);
2155 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2156 mData.mOutputVariables[location] =
2157 VariableLocation(outputVariable.name, element, outputVariableIndex);
2158 }
2159 }
2160}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002161
2162bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2163{
2164 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2165 VectorAndSamplerCount vsCounts;
2166
Jamie Madill6fa156b2015-09-22 10:17:01 -04002167 std::vector<LinkedUniform> samplerUniforms;
2168
Jamie Madill62d31cb2015-09-11 13:25:51 -04002169 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2170 {
2171 if (uniform.staticUse)
2172 {
Jamie Madill6fa156b2015-09-22 10:17:01 -04002173 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002174 }
2175 }
2176
2177 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2178 {
2179 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2180 << caps.maxVertexUniformVectors << ").";
2181 return false;
2182 }
2183
2184 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2185 {
2186 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2187 << caps.maxVertexTextureImageUnits << ").";
2188 return false;
2189 }
2190
2191 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2192 VectorAndSamplerCount fsCounts;
2193
2194 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2195 {
2196 if (uniform.staticUse)
2197 {
Jamie Madill6fa156b2015-09-22 10:17:01 -04002198 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002199 }
2200 }
2201
2202 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2203 {
2204 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2205 << caps.maxFragmentUniformVectors << ").";
2206 return false;
2207 }
2208
2209 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2210 {
2211 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2212 << caps.maxTextureImageUnits << ").";
2213 return false;
2214 }
2215
Jamie Madill6fa156b2015-09-22 10:17:01 -04002216 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2217 mSamplerUniformRange.end =
2218 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2219
2220 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2221
Jamie Madill62d31cb2015-09-11 13:25:51 -04002222 return true;
2223}
2224
2225Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill6fa156b2015-09-22 10:17:01 -04002226 const std::string &fullName,
2227 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002228{
2229 VectorAndSamplerCount vectorAndSamplerCount;
2230
2231 if (uniform.isStruct())
2232 {
2233 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2234 {
2235 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2236
2237 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2238 {
2239 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2240 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2241
Jamie Madill6fa156b2015-09-22 10:17:01 -04002242 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002243 }
2244 }
2245
2246 return vectorAndSamplerCount;
2247 }
2248
2249 // Not a struct
Jamie Madill6fa156b2015-09-22 10:17:01 -04002250 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002251 if (mData.getUniformByName(fullName) == nullptr)
2252 {
2253 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2254 uniform.arraySize, -1,
2255 sh::BlockMemberInfo::getDefaultBlockInfo());
2256 linkedUniform.staticUse = true;
Jamie Madill6fa156b2015-09-22 10:17:01 -04002257
2258 // Store sampler uniforms separately, so we'll append them to the end of the list.
2259 if (isSampler)
2260 {
2261 samplerUniforms->push_back(linkedUniform);
2262 }
2263 else
2264 {
2265 mData.mUniforms.push_back(linkedUniform);
2266 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002267 }
2268
Jamie Madill6fa156b2015-09-22 10:17:01 -04002269 unsigned int elementCount = uniform.elementCount();
2270 vectorAndSamplerCount.vectorCount = (VariableRegisterCount(uniform.type) * elementCount);
2271 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002272
2273 return vectorAndSamplerCount;
2274}
2275
2276void Program::gatherInterfaceBlockInfo()
2277{
2278 std::set<std::string> visitedList;
2279
2280 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2281
2282 ASSERT(mData.mUniformBlocks.empty());
2283 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2284 {
2285 // Only 'packed' blocks are allowed to be considered inacive.
2286 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2287 continue;
2288
2289 if (visitedList.count(vertexBlock.name) > 0)
2290 continue;
2291
2292 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2293 visitedList.insert(vertexBlock.name);
2294 }
2295
2296 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2297
2298 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2299 {
2300 // Only 'packed' blocks are allowed to be considered inacive.
2301 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2302 continue;
2303
2304 if (visitedList.count(fragmentBlock.name) > 0)
2305 {
2306 for (gl::UniformBlock &block : mData.mUniformBlocks)
2307 {
2308 if (block.name == fragmentBlock.name)
2309 {
2310 block.fragmentStaticUse = fragmentBlock.staticUse;
2311 }
2312 }
2313
2314 continue;
2315 }
2316
2317 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2318 visitedList.insert(fragmentBlock.name);
2319 }
2320}
2321
2322void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2323{
2324 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2325 size_t firstBlockUniformIndex = mData.mUniforms.size();
2326 DefineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, &mData.mUniforms);
2327 size_t lastBlockUniformIndex = mData.mUniforms.size();
2328
2329 std::vector<unsigned int> blockUniformIndexes;
2330 for (size_t blockUniformIndex = firstBlockUniformIndex;
2331 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2332 {
2333 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2334 }
2335
2336 if (interfaceBlock.arraySize > 0)
2337 {
2338 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2339 {
2340 UniformBlock block(interfaceBlock.name, true, arrayElement);
2341 block.memberUniformIndexes = blockUniformIndexes;
2342
2343 if (shaderType == GL_VERTEX_SHADER)
2344 {
2345 block.vertexStaticUse = interfaceBlock.staticUse;
2346 }
2347 else
2348 {
2349 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2350 block.fragmentStaticUse = interfaceBlock.staticUse;
2351 }
2352
2353 mData.mUniformBlocks.push_back(block);
2354 }
2355 }
2356 else
2357 {
2358 UniformBlock block(interfaceBlock.name, false, 0);
2359 block.memberUniformIndexes = blockUniformIndexes;
2360
2361 if (shaderType == GL_VERTEX_SHADER)
2362 {
2363 block.vertexStaticUse = interfaceBlock.staticUse;
2364 }
2365 else
2366 {
2367 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2368 block.fragmentStaticUse = interfaceBlock.staticUse;
2369 }
2370
2371 mData.mUniformBlocks.push_back(block);
2372 }
2373}
2374
2375template <typename T>
2376void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2377{
2378 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2379 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2380 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2381
2382 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2383 {
2384 // Do a cast conversion for boolean types. From the spec:
2385 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2386 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2387 for (GLsizei component = 0; component < count; ++component)
2388 {
2389 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2390 }
2391 }
2392 else
2393 {
Jamie Madill6fa156b2015-09-22 10:17:01 -04002394 // Invalide the validation cache if we modify the sampler data.
2395 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2396 {
2397 mCachedValidateSamplersResult.reset();
2398 }
2399
Jamie Madill62d31cb2015-09-11 13:25:51 -04002400 memcpy(destPointer, v, sizeof(T) * count);
2401 }
2402}
2403
2404template <size_t cols, size_t rows, typename T>
2405void Program::setMatrixUniformInternal(GLint location,
2406 GLsizei count,
2407 GLboolean transpose,
2408 const T *v)
2409{
2410 if (!transpose)
2411 {
2412 setUniformInternal(location, count * cols * rows, v);
2413 return;
2414 }
2415
2416 // Perform a transposing copy.
2417 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2418 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2419 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2420 for (GLsizei element = 0; element < count; ++element)
2421 {
2422 size_t elementOffset = element * rows * cols;
2423
2424 for (size_t row = 0; row < rows; ++row)
2425 {
2426 for (size_t col = 0; col < cols; ++col)
2427 {
2428 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2429 }
2430 }
2431 }
2432}
2433
2434template <typename DestT>
2435void Program::getUniformInternal(GLint location, DestT *dataOut) const
2436{
2437 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2438 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2439
2440 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2441
2442 GLenum componentType = VariableComponentType(uniform.type);
2443 if (componentType == GLTypeToGLenum<DestT>::value)
2444 {
2445 memcpy(dataOut, srcPointer, uniform.getElementSize());
2446 return;
2447 }
2448
2449 int components = VariableComponentCount(uniform.type) * uniform.elementCount();
2450
2451 switch (componentType)
2452 {
2453 case GL_INT:
2454 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2455 break;
2456 case GL_UNSIGNED_INT:
2457 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2458 break;
2459 case GL_BOOL:
2460 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2461 break;
2462 case GL_FLOAT:
2463 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2464 break;
2465 default:
2466 UNREACHABLE();
2467 }
2468}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469}