blob: 47bc368681120940fb9d4663d6e3434207331661 [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
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400177bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
178{
179 for (const LinkedUniform &uniform : list)
180 {
181 if (uniform.name == name)
182 return true;
183 }
184
185 return false;
186}
187
Jamie Madill62d31cb2015-09-11 13:25:51 -0400188} // anonymous namespace
189
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000190AttributeBindings::AttributeBindings()
191{
192}
193
194AttributeBindings::~AttributeBindings()
195{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000196}
197
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400198InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000199{
200}
201
202InfoLog::~InfoLog()
203{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000204}
205
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400206size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000207{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400208 const std::string &logString = mStream.str();
209 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000210}
211
Geoff Lange1a27752015-10-05 13:16:04 -0400212void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000213{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400214 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000215
216 if (bufSize > 0)
217 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400218 const std::string str(mStream.str());
219
220 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000221 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400222 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
223 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000224 }
225
226 infoLog[index] = '\0';
227 }
228
229 if (length)
230 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400231 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000232 }
233}
234
235// append a santized message to the program info log.
236// The D3D compiler includes a fake file path in some of the warning or error
237// messages, so lets remove all occurrences of this fake file path from the log.
238void InfoLog::appendSanitized(const char *message)
239{
240 std::string msg(message);
241
242 size_t found;
243 do
244 {
245 found = msg.find(g_fakepath);
246 if (found != std::string::npos)
247 {
248 msg.erase(found, strlen(g_fakepath));
249 }
250 }
251 while (found != std::string::npos);
252
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400253 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000254}
255
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000256void InfoLog::reset()
257{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000258}
259
Geoff Lang7dd2e102014-11-10 15:19:26 -0500260VariableLocation::VariableLocation()
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400261 : name(),
262 element(0),
263 index(0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000264{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500265}
266
267VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index)
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400268 : name(name),
269 element(element),
270 index(index)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500271{
272}
273
274LinkedVarying::LinkedVarying()
275{
276}
277
278LinkedVarying::LinkedVarying(const std::string &name, GLenum type, GLsizei size, const std::string &semanticName,
279 unsigned int semanticIndex, unsigned int semanticIndexCount)
280 : name(name), type(type), size(size), semanticName(semanticName), semanticIndex(semanticIndex), semanticIndexCount(semanticIndexCount)
281{
282}
283
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400284Program::Data::Data()
285 : mAttachedFragmentShader(nullptr),
286 mAttachedVertexShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400287 mTransformFeedbackBufferMode(GL_NONE)
288{
289}
290
291Program::Data::~Data()
292{
293 if (mAttachedVertexShader != nullptr)
294 {
295 mAttachedVertexShader->release();
296 }
297
298 if (mAttachedFragmentShader != nullptr)
299 {
300 mAttachedFragmentShader->release();
301 }
302}
303
Jamie Madill62d31cb2015-09-11 13:25:51 -0400304const LinkedUniform *Program::Data::getUniformByName(const std::string &name) const
305{
306 for (const LinkedUniform &linkedUniform : mUniforms)
307 {
308 if (linkedUniform.name == name)
309 {
310 return &linkedUniform;
311 }
312 }
313
314 return nullptr;
315}
316
317GLint Program::Data::getUniformLocation(const std::string &name) const
318{
319 size_t subscript = GL_INVALID_INDEX;
320 std::string baseName = gl::ParseUniformName(name, &subscript);
321
322 for (size_t location = 0; location < mUniformLocations.size(); ++location)
323 {
324 const VariableLocation &uniformLocation = mUniformLocations[location];
325 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
326
327 if (uniform.name == baseName)
328 {
329 if ((uniform.isArray() && uniformLocation.element == subscript) ||
330 (subscript == GL_INVALID_INDEX))
331 {
332 return static_cast<GLint>(location);
333 }
334 }
335 }
336
337 return -1;
338}
339
340GLuint Program::Data::getUniformIndex(const std::string &name) const
341{
342 size_t subscript = GL_INVALID_INDEX;
343 std::string baseName = gl::ParseUniformName(name, &subscript);
344
345 // The app is not allowed to specify array indices other than 0 for arrays of basic types
346 if (subscript != 0 && subscript != GL_INVALID_INDEX)
347 {
348 return GL_INVALID_INDEX;
349 }
350
351 for (size_t index = 0; index < mUniforms.size(); index++)
352 {
353 const LinkedUniform &uniform = mUniforms[index];
354 if (uniform.name == baseName)
355 {
356 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
357 {
358 return static_cast<GLuint>(index);
359 }
360 }
361 }
362
363 return GL_INVALID_INDEX;
364}
365
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400366Program::Program(rx::ImplFactory *factory, ResourceManager *manager, GLuint handle)
367 : mProgram(factory->createProgram(mData)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400368 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500369 mLinked(false),
370 mDeleteStatus(false),
371 mRefCount(0),
372 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400373 mHandle(handle),
374 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500375{
376 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000377
378 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500379 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380}
381
382Program::~Program()
383{
384 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000385
Geoff Lang7dd2e102014-11-10 15:19:26 -0500386 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387}
388
389bool Program::attachShader(Shader *shader)
390{
391 if (shader->getType() == GL_VERTEX_SHADER)
392 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400393 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394 {
395 return false;
396 }
397
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400398 mData.mAttachedVertexShader = shader;
399 mData.mAttachedVertexShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400 }
401 else if (shader->getType() == GL_FRAGMENT_SHADER)
402 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400403 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404 {
405 return false;
406 }
407
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400408 mData.mAttachedFragmentShader = shader;
409 mData.mAttachedFragmentShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410 }
411 else UNREACHABLE();
412
413 return true;
414}
415
416bool Program::detachShader(Shader *shader)
417{
418 if (shader->getType() == GL_VERTEX_SHADER)
419 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400420 if (mData.mAttachedVertexShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421 {
422 return false;
423 }
424
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400425 shader->release();
426 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427 }
428 else if (shader->getType() == GL_FRAGMENT_SHADER)
429 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400430 if (mData.mAttachedFragmentShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431 {
432 return false;
433 }
434
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400435 shader->release();
436 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437 }
438 else UNREACHABLE();
439
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440 return true;
441}
442
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000443int Program::getAttachedShadersCount() const
444{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400445 return (mData.mAttachedVertexShader ? 1 : 0) + (mData.mAttachedFragmentShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000446}
447
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000448void AttributeBindings::bindAttributeLocation(GLuint index, const char *name)
449{
450 if (index < MAX_VERTEX_ATTRIBS)
451 {
452 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
453 {
454 mAttributeBinding[i].erase(name);
455 }
456
457 mAttributeBinding[index].insert(name);
458 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000459}
460
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461void Program::bindAttributeLocation(GLuint index, const char *name)
462{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000463 mAttributeBindings.bindAttributeLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464}
465
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
467// compiling them into binaries, determining the attribute mappings, and collecting
468// a list of uniforms
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400469Error Program::link(const gl::Data &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000470{
471 unlink(false);
472
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000473 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000474 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000475
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400476 if (!mData.mAttachedFragmentShader || !mData.mAttachedFragmentShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500477 {
478 return Error(GL_NO_ERROR);
479 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400480 ASSERT(mData.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500481
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400482 if (!mData.mAttachedVertexShader || !mData.mAttachedVertexShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500483 {
484 return Error(GL_NO_ERROR);
485 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400486 ASSERT(mData.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500487
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400488 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mData.mAttachedVertexShader))
Jamie Madill437d2662014-12-05 14:23:35 -0500489 {
490 return Error(GL_NO_ERROR);
491 }
492
Jamie Madillada9ecc2015-08-17 12:53:37 -0400493 if (!linkVaryings(mInfoLog, mData.mAttachedVertexShader, mData.mAttachedFragmentShader))
494 {
495 return Error(GL_NO_ERROR);
496 }
497
Jamie Madillea918db2015-08-18 14:48:59 -0400498 if (!linkUniforms(mInfoLog, *data.caps))
499 {
500 return Error(GL_NO_ERROR);
501 }
502
Jamie Madille473dee2015-08-18 14:49:01 -0400503 if (!linkUniformBlocks(mInfoLog, *data.caps))
504 {
505 return Error(GL_NO_ERROR);
506 }
507
Jamie Madillccdf74b2015-08-18 10:46:12 -0400508 const auto &mergedVaryings = getMergedVaryings();
509
510 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, *data.caps))
511 {
512 return Error(GL_NO_ERROR);
513 }
514
Jamie Madill80a6fc02015-08-21 16:53:16 -0400515 linkOutputVariables();
516
Jamie Madillf5f4ad22015-09-02 18:32:38 +0000517 rx::LinkResult result = mProgram->link(data, mInfoLog);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500518 if (result.error.isError() || !result.linkSuccess)
519 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500520 return result.error;
521 }
522
Jamie Madillccdf74b2015-08-18 10:46:12 -0400523 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400524 mProgram->gatherUniformBlockInfo(&mData.mUniformBlocks, &mData.mUniforms);
Jamie Madillccdf74b2015-08-18 10:46:12 -0400525
Geoff Lang7dd2e102014-11-10 15:19:26 -0500526 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400527 return gl::Error(GL_NO_ERROR);
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000528}
529
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000530int AttributeBindings::getAttributeBinding(const std::string &name) const
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000531{
532 for (int location = 0; location < MAX_VERTEX_ATTRIBS; location++)
533 {
534 if (mAttributeBinding[location].find(name) != mAttributeBinding[location].end())
535 {
536 return location;
537 }
538 }
539
540 return -1;
541}
542
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000543// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544void Program::unlink(bool destroy)
545{
546 if (destroy) // Object being destructed
547 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400548 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400550 mData.mAttachedFragmentShader->release();
551 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552 }
553
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400554 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400556 mData.mAttachedVertexShader->release();
557 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000558 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000559 }
560
Jamie Madillc349ec02015-08-21 16:53:12 -0400561 mData.mAttributes.clear();
Jamie Madill63805b42015-08-25 13:17:39 -0400562 mData.mActiveAttribLocationsMask.reset();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400563 mData.mTransformFeedbackVaryingVars.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400564 mData.mUniforms.clear();
565 mData.mUniformLocations.clear();
566 mData.mUniformBlocks.clear();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400567 mData.mOutputVariables.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500568
Geoff Lang7dd2e102014-11-10 15:19:26 -0500569 mValidated = false;
570
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000571 mLinked = false;
572}
573
Geoff Lange1a27752015-10-05 13:16:04 -0400574bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000575{
576 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577}
578
Geoff Lang7dd2e102014-11-10 15:19:26 -0500579Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000580{
581 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000582
Geoff Lang7dd2e102014-11-10 15:19:26 -0500583#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
584 return Error(GL_NO_ERROR);
585#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400586 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
587 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000588 {
Jamie Madillf6113162015-05-07 11:49:21 -0400589 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500590 return Error(GL_NO_ERROR);
591 }
592
Geoff Langc46cc2f2015-10-01 17:16:20 -0400593 BinaryInputStream stream(binary, length);
594
Geoff Lang7dd2e102014-11-10 15:19:26 -0500595 int majorVersion = stream.readInt<int>();
596 int minorVersion = stream.readInt<int>();
597 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
598 {
Jamie Madillf6113162015-05-07 11:49:21 -0400599 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500600 return Error(GL_NO_ERROR);
601 }
602
603 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
604 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
605 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
606 {
Jamie Madillf6113162015-05-07 11:49:21 -0400607 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500608 return Error(GL_NO_ERROR);
609 }
610
Jamie Madill63805b42015-08-25 13:17:39 -0400611 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
612 "Too many vertex attribs for mask");
613 mData.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500614
Jamie Madill3da79b72015-04-27 11:09:17 -0400615 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400616 ASSERT(mData.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400617 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
618 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400619 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400620 LoadShaderVar(&stream, &attrib);
621 attrib.location = stream.readInt<int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400622 mData.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400623 }
624
Jamie Madill62d31cb2015-09-11 13:25:51 -0400625 unsigned int uniformCount = stream.readInt<unsigned int>();
626 ASSERT(mData.mUniforms.empty());
627 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
628 {
629 LinkedUniform uniform;
630 LoadShaderVar(&stream, &uniform);
631
632 uniform.blockIndex = stream.readInt<int>();
633 uniform.blockInfo.offset = stream.readInt<int>();
634 uniform.blockInfo.arrayStride = stream.readInt<int>();
635 uniform.blockInfo.matrixStride = stream.readInt<int>();
636 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
637
638 mData.mUniforms.push_back(uniform);
639 }
640
641 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
642 ASSERT(mData.mUniformLocations.empty());
643 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
644 uniformIndexIndex++)
645 {
646 VariableLocation variable;
647 stream.readString(&variable.name);
648 stream.readInt(&variable.element);
649 stream.readInt(&variable.index);
650
651 mData.mUniformLocations.push_back(variable);
652 }
653
654 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
655 ASSERT(mData.mUniformBlocks.empty());
656 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
657 ++uniformBlockIndex)
658 {
659 UniformBlock uniformBlock;
660 stream.readString(&uniformBlock.name);
661 stream.readBool(&uniformBlock.isArray);
662 stream.readInt(&uniformBlock.arrayElement);
663 stream.readInt(&uniformBlock.dataSize);
664 stream.readBool(&uniformBlock.vertexStaticUse);
665 stream.readBool(&uniformBlock.fragmentStaticUse);
666
667 unsigned int numMembers = stream.readInt<unsigned int>();
668 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
669 {
670 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
671 }
672
673 // TODO(jmadill): Make D3D-only
674 stream.readInt(&uniformBlock.psRegisterIndex);
675 stream.readInt(&uniformBlock.vsRegisterIndex);
676
677 mData.mUniformBlocks.push_back(uniformBlock);
678 }
679
Jamie Madillada9ecc2015-08-17 12:53:37 -0400680 stream.readInt(&mData.mTransformFeedbackBufferMode);
681
Jamie Madill80a6fc02015-08-21 16:53:16 -0400682 unsigned int outputVarCount = stream.readInt<unsigned int>();
683 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
684 {
685 int locationIndex = stream.readInt<int>();
686 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400687 stream.readInt(&locationData.element);
688 stream.readInt(&locationData.index);
689 stream.readString(&locationData.name);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400690 mData.mOutputVariables[locationIndex] = locationData;
691 }
692
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400693 stream.readInt(&mSamplerUniformRange.start);
694 stream.readInt(&mSamplerUniformRange.end);
695
Geoff Lang7dd2e102014-11-10 15:19:26 -0500696 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
697 if (result.error.isError() || !result.linkSuccess)
698 {
Geoff Langb543aff2014-09-30 14:52:54 -0400699 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000700 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000701
Geoff Lang7dd2e102014-11-10 15:19:26 -0500702 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400703 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500704#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
705}
706
707Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
708{
709 if (binaryFormat)
710 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400711 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500712 }
713
714 BinaryOutputStream stream;
715
Geoff Lang7dd2e102014-11-10 15:19:26 -0500716 stream.writeInt(ANGLE_MAJOR_VERSION);
717 stream.writeInt(ANGLE_MINOR_VERSION);
718 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
719
Jamie Madill63805b42015-08-25 13:17:39 -0400720 stream.writeInt(mData.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500721
Jamie Madillc349ec02015-08-21 16:53:12 -0400722 stream.writeInt(mData.mAttributes.size());
723 for (const sh::Attribute &attrib : mData.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400724 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400725 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400726 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400727 }
728
729 stream.writeInt(mData.mUniforms.size());
730 for (const gl::LinkedUniform &uniform : mData.mUniforms)
731 {
732 WriteShaderVar(&stream, uniform);
733
734 // FIXME: referenced
735
736 stream.writeInt(uniform.blockIndex);
737 stream.writeInt(uniform.blockInfo.offset);
738 stream.writeInt(uniform.blockInfo.arrayStride);
739 stream.writeInt(uniform.blockInfo.matrixStride);
740 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
741 }
742
743 stream.writeInt(mData.mUniformLocations.size());
744 for (const auto &variable : mData.mUniformLocations)
745 {
746 stream.writeString(variable.name);
747 stream.writeInt(variable.element);
748 stream.writeInt(variable.index);
749 }
750
751 stream.writeInt(mData.mUniformBlocks.size());
752 for (const UniformBlock &uniformBlock : mData.mUniformBlocks)
753 {
754 stream.writeString(uniformBlock.name);
755 stream.writeInt(uniformBlock.isArray);
756 stream.writeInt(uniformBlock.arrayElement);
757 stream.writeInt(uniformBlock.dataSize);
758
759 stream.writeInt(uniformBlock.vertexStaticUse);
760 stream.writeInt(uniformBlock.fragmentStaticUse);
761
762 stream.writeInt(uniformBlock.memberUniformIndexes.size());
763 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
764 {
765 stream.writeInt(memberUniformIndex);
766 }
767
768 // TODO(jmadill): make D3D-only
769 stream.writeInt(uniformBlock.psRegisterIndex);
770 stream.writeInt(uniformBlock.vsRegisterIndex);
Jamie Madill3da79b72015-04-27 11:09:17 -0400771 }
772
Jamie Madillada9ecc2015-08-17 12:53:37 -0400773 stream.writeInt(mData.mTransformFeedbackBufferMode);
774
Jamie Madill80a6fc02015-08-21 16:53:16 -0400775 stream.writeInt(mData.mOutputVariables.size());
776 for (const auto &outputPair : mData.mOutputVariables)
777 {
778 stream.writeInt(outputPair.first);
779 stream.writeInt(outputPair.second.element);
780 stream.writeInt(outputPair.second.index);
781 stream.writeString(outputPair.second.name);
782 }
783
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400784 stream.writeInt(mSamplerUniformRange.start);
785 stream.writeInt(mSamplerUniformRange.end);
786
Geoff Lang7dd2e102014-11-10 15:19:26 -0500787 gl::Error error = mProgram->save(&stream);
788 if (error.isError())
789 {
790 return error;
791 }
792
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700793 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500794 const void *streamData = stream.data();
795
796 if (streamLength > bufSize)
797 {
798 if (length)
799 {
800 *length = 0;
801 }
802
803 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
804 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
805 // sizes and then copy it.
806 return Error(GL_INVALID_OPERATION);
807 }
808
809 if (binary)
810 {
811 char *ptr = reinterpret_cast<char*>(binary);
812
813 memcpy(ptr, streamData, streamLength);
814 ptr += streamLength;
815
816 ASSERT(ptr - streamLength == binary);
817 }
818
819 if (length)
820 {
821 *length = streamLength;
822 }
823
824 return Error(GL_NO_ERROR);
825}
826
827GLint Program::getBinaryLength() const
828{
829 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400830 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500831 if (error.isError())
832 {
833 return 0;
834 }
835
836 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000837}
838
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000839void Program::release()
840{
841 mRefCount--;
842
843 if (mRefCount == 0 && mDeleteStatus)
844 {
845 mResourceManager->deleteProgram(mHandle);
846 }
847}
848
849void Program::addRef()
850{
851 mRefCount++;
852}
853
854unsigned int Program::getRefCount() const
855{
856 return mRefCount;
857}
858
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000859int Program::getInfoLogLength() const
860{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400861 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000862}
863
Geoff Lange1a27752015-10-05 13:16:04 -0400864void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000865{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000866 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000867}
868
Geoff Lange1a27752015-10-05 13:16:04 -0400869void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000870{
871 int total = 0;
872
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400873 if (mData.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000874 {
875 if (total < maxCount)
876 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400877 shaders[total] = mData.mAttachedVertexShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000878 }
879
880 total++;
881 }
882
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400883 if (mData.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000884 {
885 if (total < maxCount)
886 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400887 shaders[total] = mData.mAttachedFragmentShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000888 }
889
890 total++;
891 }
892
893 if (count)
894 {
895 *count = total;
896 }
897}
898
Geoff Lange1a27752015-10-05 13:16:04 -0400899GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500900{
Jamie Madillc349ec02015-08-21 16:53:12 -0400901 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500902 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400903 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500904 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400905 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500906 }
907 }
908
Austin Kinrossb8af7232015-03-16 22:33:25 -0700909 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500910}
911
Jamie Madill63805b42015-08-25 13:17:39 -0400912bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400913{
Olli Etuaho401d9fe2015-08-26 10:19:59 +0300914 ASSERT(attribLocation < mData.mActiveAttribLocationsMask.size());
Jamie Madill63805b42015-08-25 13:17:39 -0400915 return mData.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500916}
917
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000918void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
919{
Jamie Madillc349ec02015-08-21 16:53:12 -0400920 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000921 {
922 if (bufsize > 0)
923 {
924 name[0] = '\0';
925 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500926
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000927 if (length)
928 {
929 *length = 0;
930 }
931
932 *type = GL_NONE;
933 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400934 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000935 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400936
937 size_t attributeIndex = 0;
938
939 for (const sh::Attribute &attribute : mData.mAttributes)
940 {
941 // Skip over inactive attributes
942 if (attribute.staticUse)
943 {
944 if (static_cast<size_t>(index) == attributeIndex)
945 {
946 break;
947 }
948 attributeIndex++;
949 }
950 }
951
952 ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
953 const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
954
955 if (bufsize > 0)
956 {
957 const char *string = attrib.name.c_str();
958
959 strncpy(name, string, bufsize);
960 name[bufsize - 1] = '\0';
961
962 if (length)
963 {
964 *length = static_cast<GLsizei>(strlen(name));
965 }
966 }
967
968 // Always a single 'type' instance
969 *size = 1;
970 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000971}
972
Geoff Lange1a27752015-10-05 13:16:04 -0400973GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000974{
Jamie Madillc349ec02015-08-21 16:53:12 -0400975 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400976 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400977 return 0;
978 }
979
980 GLint count = 0;
981
982 for (const sh::Attribute &attrib : mData.mAttributes)
983 {
984 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000985 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500986
987 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000988}
989
Geoff Lange1a27752015-10-05 13:16:04 -0400990GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000991{
Jamie Madillc349ec02015-08-21 16:53:12 -0400992 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400993 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400994 return 0;
995 }
996
997 size_t maxLength = 0;
998
999 for (const sh::Attribute &attrib : mData.mAttributes)
1000 {
1001 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001002 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001003 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001004 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001005 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001006
Jamie Madillc349ec02015-08-21 16:53:12 -04001007 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001008}
1009
Geoff Lang7dd2e102014-11-10 15:19:26 -05001010GLint Program::getFragDataLocation(const std::string &name) const
1011{
1012 std::string baseName(name);
1013 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001014 for (auto outputPair : mData.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001015 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001016 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001017 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1018 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001019 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001020 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001021 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001022 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001023}
1024
Geoff Lange1a27752015-10-05 13:16:04 -04001025void Program::getActiveUniform(GLuint index,
1026 GLsizei bufsize,
1027 GLsizei *length,
1028 GLint *size,
1029 GLenum *type,
1030 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001031{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001032 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001033 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001034 // index must be smaller than getActiveUniformCount()
1035 ASSERT(index < mData.mUniforms.size());
1036 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001037
1038 if (bufsize > 0)
1039 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001040 std::string string = uniform.name;
1041 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001042 {
1043 string += "[0]";
1044 }
1045
1046 strncpy(name, string.c_str(), bufsize);
1047 name[bufsize - 1] = '\0';
1048
1049 if (length)
1050 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001051 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001052 }
1053 }
1054
Jamie Madill62d31cb2015-09-11 13:25:51 -04001055 *size = uniform.elementCount();
1056 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001057 }
1058 else
1059 {
1060 if (bufsize > 0)
1061 {
1062 name[0] = '\0';
1063 }
1064
1065 if (length)
1066 {
1067 *length = 0;
1068 }
1069
1070 *size = 0;
1071 *type = GL_NONE;
1072 }
1073}
1074
Geoff Lange1a27752015-10-05 13:16:04 -04001075GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001076{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001077 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001078 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001079 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001080 }
1081 else
1082 {
1083 return 0;
1084 }
1085}
1086
Geoff Lange1a27752015-10-05 13:16:04 -04001087GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001088{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001089 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090
1091 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001092 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001093 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001094 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001095 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001096 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001097 size_t length = uniform.name.length() + 1u;
1098 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099 {
1100 length += 3; // Counting in "[0]".
1101 }
1102 maxLength = std::max(length, maxLength);
1103 }
1104 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001105 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106
Jamie Madill62d31cb2015-09-11 13:25:51 -04001107 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001108}
1109
1110GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1111{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001112 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1113 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001114 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001115 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001116 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1117 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1118 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1119 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1120 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1121 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1122 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1123 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1124 default:
1125 UNREACHABLE();
1126 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001127 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001128 return 0;
1129}
1130
1131bool Program::isValidUniformLocation(GLint location) const
1132{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001133 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
1134 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001135}
1136
Jamie Madill62d31cb2015-09-11 13:25:51 -04001137const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001138{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001139 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1140 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001141}
1142
Jamie Madill62d31cb2015-09-11 13:25:51 -04001143GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001145 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001146}
1147
Jamie Madill62d31cb2015-09-11 13:25:51 -04001148GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001149{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001150 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001151}
1152
1153void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1154{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001155 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001156 mProgram->setUniform1fv(location, count, v);
1157}
1158
1159void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1160{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001161 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001162 mProgram->setUniform2fv(location, count, v);
1163}
1164
1165void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1166{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001167 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001168 mProgram->setUniform3fv(location, count, v);
1169}
1170
1171void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1172{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001173 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001174 mProgram->setUniform4fv(location, count, v);
1175}
1176
1177void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1178{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001179 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001180 mProgram->setUniform1iv(location, count, v);
1181}
1182
1183void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1184{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001185 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001186 mProgram->setUniform2iv(location, count, v);
1187}
1188
1189void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1190{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001191 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001192 mProgram->setUniform3iv(location, count, v);
1193}
1194
1195void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1196{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001197 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001198 mProgram->setUniform4iv(location, count, v);
1199}
1200
1201void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1202{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001203 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001204 mProgram->setUniform1uiv(location, count, v);
1205}
1206
1207void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1208{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001209 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001210 mProgram->setUniform2uiv(location, count, v);
1211}
1212
1213void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1214{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001215 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001216 mProgram->setUniform3uiv(location, count, v);
1217}
1218
1219void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1220{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001221 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001222 mProgram->setUniform4uiv(location, count, v);
1223}
1224
1225void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1226{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001227 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001228 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1229}
1230
1231void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1232{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001233 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001234 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1235}
1236
1237void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1238{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001239 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001240 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1241}
1242
1243void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1244{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001245 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001246 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1247}
1248
1249void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1250{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001251 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001252 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1253}
1254
1255void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1256{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001257 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001258 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1259}
1260
1261void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1262{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001263 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001264 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1265}
1266
1267void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1268{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001269 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001270 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1271}
1272
1273void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1274{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001275 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001276 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1277}
1278
Geoff Lange1a27752015-10-05 13:16:04 -04001279void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001281 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001282}
1283
Geoff Lange1a27752015-10-05 13:16:04 -04001284void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001285{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001286 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287}
1288
Geoff Lange1a27752015-10-05 13:16:04 -04001289void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001291 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292}
1293
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001294void Program::flagForDeletion()
1295{
1296 mDeleteStatus = true;
1297}
1298
1299bool Program::isFlaggedForDeletion() const
1300{
1301 return mDeleteStatus;
1302}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001303
Brandon Jones43a53e22014-08-28 16:23:22 -07001304void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001305{
1306 mInfoLog.reset();
1307
Geoff Lang7dd2e102014-11-10 15:19:26 -05001308 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001309 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001310 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001311 }
1312 else
1313 {
Jamie Madillf6113162015-05-07 11:49:21 -04001314 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001315 }
1316}
1317
Geoff Lang7dd2e102014-11-10 15:19:26 -05001318bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1319{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001320 // Skip cache if we're using an infolog, so we get the full error.
1321 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1322 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1323 {
1324 return mCachedValidateSamplersResult.value();
1325 }
1326
1327 if (mTextureUnitTypesCache.empty())
1328 {
1329 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1330 }
1331 else
1332 {
1333 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1334 }
1335
1336 // if any two active samplers in a program are of different types, but refer to the same
1337 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1338 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1339 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1340 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1341 {
1342 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1343 ASSERT(uniform.isSampler());
1344
1345 if (!uniform.staticUse)
1346 continue;
1347
1348 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1349 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1350
1351 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1352 {
1353 GLuint textureUnit = dataPtr[arrayElement];
1354
1355 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1356 {
1357 if (infoLog)
1358 {
1359 (*infoLog) << "Sampler uniform (" << textureUnit
1360 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1361 << caps.maxCombinedTextureImageUnits << ")";
1362 }
1363
1364 mCachedValidateSamplersResult = false;
1365 return false;
1366 }
1367
1368 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1369 {
1370 if (textureType != mTextureUnitTypesCache[textureUnit])
1371 {
1372 if (infoLog)
1373 {
1374 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1375 "image unit ("
1376 << textureUnit << ").";
1377 }
1378
1379 mCachedValidateSamplersResult = false;
1380 return false;
1381 }
1382 }
1383 else
1384 {
1385 mTextureUnitTypesCache[textureUnit] = textureType;
1386 }
1387 }
1388 }
1389
1390 mCachedValidateSamplersResult = true;
1391 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001392}
1393
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001394bool Program::isValidated() const
1395{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001396 return mValidated;
1397}
1398
Geoff Lange1a27752015-10-05 13:16:04 -04001399GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001400{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001401 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402}
1403
1404void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1405{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001406 ASSERT(uniformBlockIndex <
1407 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408
Jamie Madill62d31cb2015-09-11 13:25:51 -04001409 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001410
1411 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001412 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413 std::string string = uniformBlock.name;
1414
Jamie Madill62d31cb2015-09-11 13:25:51 -04001415 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001417 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001418 }
1419
1420 strncpy(uniformBlockName, string.c_str(), bufSize);
1421 uniformBlockName[bufSize - 1] = '\0';
1422
1423 if (length)
1424 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001425 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001427 }
1428}
1429
Geoff Lang7dd2e102014-11-10 15:19:26 -05001430void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001431{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001432 ASSERT(uniformBlockIndex <
1433 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434
Jamie Madill62d31cb2015-09-11 13:25:51 -04001435 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001436
1437 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001438 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439 case GL_UNIFORM_BLOCK_DATA_SIZE:
1440 *params = static_cast<GLint>(uniformBlock.dataSize);
1441 break;
1442 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001443 *params =
1444 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445 break;
1446 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1447 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1448 break;
1449 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1450 {
1451 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1452 {
1453 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1454 }
1455 }
1456 break;
1457 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001458 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001459 break;
1460 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001461 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462 break;
1463 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001464 }
1465}
1466
Geoff Lange1a27752015-10-05 13:16:04 -04001467GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001468{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469 int maxLength = 0;
1470
1471 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001472 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001473 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1475 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001476 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477 if (!uniformBlock.name.empty())
1478 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001479 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480
1481 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001482 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483
1484 maxLength = std::max(length + arrayLength, maxLength);
1485 }
1486 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001487 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001488
1489 return maxLength;
1490}
1491
Geoff Lange1a27752015-10-05 13:16:04 -04001492GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001493{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001494 size_t subscript = GL_INVALID_INDEX;
1495 std::string baseName = gl::ParseUniformName(name, &subscript);
1496
1497 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1498 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1499 {
1500 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1501 if (uniformBlock.name == baseName)
1502 {
1503 const bool arrayElementZero =
1504 (subscript == GL_INVALID_INDEX &&
1505 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1506 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1507 {
1508 return blockIndex;
1509 }
1510 }
1511 }
1512
1513 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001514}
1515
Jamie Madill62d31cb2015-09-11 13:25:51 -04001516const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001517{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001518 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1519 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001520}
1521
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001522void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1523{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001524 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001525 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001526}
1527
1528GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1529{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001530 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001531}
1532
1533void Program::resetUniformBlockBindings()
1534{
1535 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1536 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001537 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001538 }
Geoff Lang5d124a62015-09-15 13:03:27 -04001539 mData.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001540}
1541
Geoff Lang48dcae72014-02-05 16:28:24 -05001542void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1543{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001544 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001545 for (GLsizei i = 0; i < count; i++)
1546 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001547 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001548 }
1549
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001550 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001551}
1552
1553void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1554{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001555 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001556 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001557 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1558 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001559 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1560 if (length)
1561 {
1562 *length = lastNameIdx;
1563 }
1564 if (size)
1565 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001566 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001567 }
1568 if (type)
1569 {
1570 *type = varying.type;
1571 }
1572 if (name)
1573 {
1574 memcpy(name, varying.name.c_str(), lastNameIdx);
1575 name[lastNameIdx] = '\0';
1576 }
1577 }
1578}
1579
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001580GLsizei Program::getTransformFeedbackVaryingCount() const
1581{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001582 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001583 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001584 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001585 }
1586 else
1587 {
1588 return 0;
1589 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001590}
1591
1592GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1593{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001594 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001595 {
1596 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001597 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001598 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001599 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1600 }
1601
1602 return maxSize;
1603 }
1604 else
1605 {
1606 return 0;
1607 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001608}
1609
1610GLenum Program::getTransformFeedbackBufferMode() const
1611{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001612 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001613}
1614
Jamie Madillada9ecc2015-08-17 12:53:37 -04001615// static
1616bool Program::linkVaryings(InfoLog &infoLog,
1617 const Shader *vertexShader,
1618 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001619{
Jamie Madill4cff2472015-08-21 16:53:18 -04001620 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1621 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001622
Jamie Madill4cff2472015-08-21 16:53:18 -04001623 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625 bool matched = false;
1626
1627 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001628 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629 {
1630 continue;
1631 }
1632
Jamie Madill4cff2472015-08-21 16:53:18 -04001633 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001634 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001635 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001636 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001637 ASSERT(!input.isBuiltIn());
1638 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001639 {
1640 return false;
1641 }
1642
Geoff Lang7dd2e102014-11-10 15:19:26 -05001643 matched = true;
1644 break;
1645 }
1646 }
1647
1648 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001649 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001651 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001652 return false;
1653 }
1654 }
1655
Jamie Madillada9ecc2015-08-17 12:53:37 -04001656 // TODO(jmadill): verify no unmatched vertex varyings?
1657
Geoff Lang7dd2e102014-11-10 15:19:26 -05001658 return true;
1659}
1660
Jamie Madill62d31cb2015-09-11 13:25:51 -04001661bool Program::linkUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Jamie Madillea918db2015-08-18 14:48:59 -04001662{
1663 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1664 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1665
1666 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001667 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001668
1669 for (const sh::Uniform &vertexUniform : vertexUniforms)
1670 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001671 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001672 }
1673
1674 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1675 {
1676 auto entry = linkedUniforms.find(fragmentUniform.name);
1677 if (entry != linkedUniforms.end())
1678 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001679 LinkedUniform *vertexUniform = &entry->second;
1680 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1681 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001682 {
1683 return false;
1684 }
1685 }
1686 }
1687
Jamie Madill62d31cb2015-09-11 13:25:51 -04001688 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1689 // Also check the maximum uniform vector and sampler counts.
1690 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1691 {
1692 return false;
1693 }
1694
1695 indexUniforms();
1696
Jamie Madillea918db2015-08-18 14:48:59 -04001697 return true;
1698}
1699
Jamie Madill62d31cb2015-09-11 13:25:51 -04001700void Program::indexUniforms()
1701{
1702 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1703 {
1704 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1705
1706 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1707 {
1708 if (!uniform.isBuiltIn())
1709 {
1710 // Assign in-order uniform locations
1711 mData.mUniformLocations.push_back(gl::VariableLocation(
1712 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)));
1713 }
1714 }
1715 }
1716}
1717
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1719{
1720 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
1721 {
1722 return false;
1723 }
1724
1725 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1726 {
Jamie Madillf6113162015-05-07 11:49:21 -04001727 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001728 return false;
1729 }
1730
1731 return true;
1732}
1733
1734// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001735bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001736 InfoLog &infoLog,
1737 const AttributeBindings &attributeBindings,
1738 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001739{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001740 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001741 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001742 GLuint maxAttribs = data.caps->maxVertexAttributes;
1743
1744 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001745 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001746 {
Jamie Madillf6113162015-05-07 11:49:21 -04001747 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001748 return false;
1749 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001750
Jamie Madillc349ec02015-08-21 16:53:12 -04001751 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001752
Jamie Madillc349ec02015-08-21 16:53:12 -04001753 // Link attributes that have a binding location
1754 for (sh::Attribute &attribute : mData.mAttributes)
1755 {
1756 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001757 ASSERT(attribute.staticUse);
1758
Jamie Madillc349ec02015-08-21 16:53:12 -04001759 int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
1760 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001761 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001762 attribute.location = bindingLocation;
1763 }
1764
1765 if (attribute.location != -1)
1766 {
1767 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001768 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001769
Jamie Madill63805b42015-08-25 13:17:39 -04001770 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001771 {
Jamie Madillf6113162015-05-07 11:49:21 -04001772 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001773 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001774
1775 return false;
1776 }
1777
Jamie Madill63805b42015-08-25 13:17:39 -04001778 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001779 {
Jamie Madill63805b42015-08-25 13:17:39 -04001780 const int regLocation = attribute.location + reg;
1781 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782
1783 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001784 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001785 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001786 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001787 // TODO(jmadill): fix aliasing on ES2
1788 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001790 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001791 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792 return false;
1793 }
1794 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001795 else
1796 {
Jamie Madill63805b42015-08-25 13:17:39 -04001797 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001798 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001799
Jamie Madill63805b42015-08-25 13:17:39 -04001800 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801 }
1802 }
1803 }
1804
1805 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001806 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001808 ASSERT(attribute.staticUse);
1809
Jamie Madillc349ec02015-08-21 16:53:12 -04001810 // Not set by glBindAttribLocation or by location layout qualifier
1811 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001812 {
Jamie Madill63805b42015-08-25 13:17:39 -04001813 int regs = VariableRegisterCount(attribute.type);
1814 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001815
Jamie Madill63805b42015-08-25 13:17:39 -04001816 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001817 {
Jamie Madillf6113162015-05-07 11:49:21 -04001818 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001819 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001820 }
1821
Jamie Madillc349ec02015-08-21 16:53:12 -04001822 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823 }
1824 }
1825
Jamie Madillc349ec02015-08-21 16:53:12 -04001826 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001827 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001828 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001829 ASSERT(attribute.location != -1);
1830 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001831
Jamie Madill63805b42015-08-25 13:17:39 -04001832 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833 {
Jamie Madill63805b42015-08-25 13:17:39 -04001834 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001835 }
1836 }
1837
Geoff Lang7dd2e102014-11-10 15:19:26 -05001838 return true;
1839}
1840
Jamie Madille473dee2015-08-18 14:49:01 -04001841bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842{
Jamie Madille473dee2015-08-18 14:49:01 -04001843 const Shader &vertexShader = *mData.mAttachedVertexShader;
1844 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1845
Geoff Lang7dd2e102014-11-10 15:19:26 -05001846 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1847 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001848
Geoff Lang7dd2e102014-11-10 15:19:26 -05001849 // Check that interface blocks defined in the vertex and fragment shaders are identical
1850 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1851 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001852
1853 GLuint vertexBlockCount = 0;
1854 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001855 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001856 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001857
1858 // Note: shared and std140 layouts are always considered active
1859 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1860 {
1861 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1862 {
1863 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1864 << caps.maxVertexUniformBlocks << ")";
1865 return false;
1866 }
1867 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 }
Jamie Madille473dee2015-08-18 14:49:01 -04001869
1870 GLuint fragmentBlockCount = 0;
1871 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001872 {
Jamie Madille473dee2015-08-18 14:49:01 -04001873 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 if (entry != linkedUniformBlocks.end())
1875 {
1876 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1877 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1878 {
1879 return false;
1880 }
1881 }
Jamie Madille473dee2015-08-18 14:49:01 -04001882
Geoff Lang7dd2e102014-11-10 15:19:26 -05001883 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001884 if (fragmentInterfaceBlock.staticUse ||
1885 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001886 {
Jamie Madille473dee2015-08-18 14:49:01 -04001887 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001888 {
Jamie Madille473dee2015-08-18 14:49:01 -04001889 infoLog
1890 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1891 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892 return false;
1893 }
1894 }
1895 }
Jamie Madille473dee2015-08-18 14:49:01 -04001896
Jamie Madill62d31cb2015-09-11 13:25:51 -04001897 gatherInterfaceBlockInfo();
1898
Geoff Lang7dd2e102014-11-10 15:19:26 -05001899 return true;
1900}
1901
1902bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1903 const sh::InterfaceBlock &fragmentInterfaceBlock)
1904{
1905 const char* blockName = vertexInterfaceBlock.name.c_str();
1906 // validate blocks for the same member types
1907 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1908 {
Jamie Madillf6113162015-05-07 11:49:21 -04001909 infoLog << "Types for interface block '" << blockName
1910 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001911 return false;
1912 }
1913 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1914 {
Jamie Madillf6113162015-05-07 11:49:21 -04001915 infoLog << "Array sizes differ for interface block '" << blockName
1916 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001917 return false;
1918 }
1919 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
1920 {
Jamie Madillf6113162015-05-07 11:49:21 -04001921 infoLog << "Layout qualifiers differ for interface block '" << blockName
1922 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923 return false;
1924 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001925 const unsigned int numBlockMembers =
1926 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001927 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1928 {
1929 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
1930 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
1931 if (vertexMember.name != fragmentMember.name)
1932 {
Jamie Madillf6113162015-05-07 11:49:21 -04001933 infoLog << "Name mismatch for field " << blockMemberIndex
1934 << " of interface block '" << blockName
1935 << "': (in vertex: '" << vertexMember.name
1936 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001937 return false;
1938 }
1939 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
1940 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
1941 {
1942 return false;
1943 }
1944 }
1945 return true;
1946}
1947
1948bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
1949 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
1950{
1951 if (vertexVariable.type != fragmentVariable.type)
1952 {
Jamie Madillf6113162015-05-07 11:49:21 -04001953 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 return false;
1955 }
1956 if (vertexVariable.arraySize != fragmentVariable.arraySize)
1957 {
Jamie Madillf6113162015-05-07 11:49:21 -04001958 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959 return false;
1960 }
1961 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
1962 {
Jamie Madillf6113162015-05-07 11:49:21 -04001963 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001964 return false;
1965 }
1966
1967 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
1968 {
Jamie Madillf6113162015-05-07 11:49:21 -04001969 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970 return false;
1971 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001972 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001973 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1974 {
1975 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
1976 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
1977
1978 if (vertexMember.name != fragmentMember.name)
1979 {
Jamie Madillf6113162015-05-07 11:49:21 -04001980 infoLog << "Name mismatch for field '" << memberIndex
1981 << "' of " << variableName
1982 << ": (in vertex: '" << vertexMember.name
1983 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001984 return false;
1985 }
1986
1987 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
1988 vertexMember.name + "'";
1989
1990 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
1991 {
1992 return false;
1993 }
1994 }
1995
1996 return true;
1997}
1998
1999bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2000{
Cooper Partin1acf4382015-06-12 12:38:57 -07002001#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2002 const bool validatePrecision = true;
2003#else
2004 const bool validatePrecision = false;
2005#endif
2006
2007 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002008 {
2009 return false;
2010 }
2011
2012 return true;
2013}
2014
2015bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
2016{
2017 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2018 {
2019 return false;
2020 }
2021
Jamie Madille9cc4692015-02-19 16:00:13 -05002022 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002023 {
Jamie Madillf6113162015-05-07 11:49:21 -04002024 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002025 return false;
2026 }
2027
2028 return true;
2029}
2030
Jamie Madillccdf74b2015-08-18 10:46:12 -04002031bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2032 const std::vector<const sh::Varying *> &varyings,
2033 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034{
2035 size_t totalComponents = 0;
2036
Jamie Madillccdf74b2015-08-18 10:46:12 -04002037 std::set<std::string> uniqueNames;
2038
2039 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040 {
2041 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002042 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002044 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002046 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002047 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002048 infoLog << "Two transform feedback varyings specify the same output variable ("
2049 << tfVaryingName << ").";
2050 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002051 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002052 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053
Jamie Madillccdf74b2015-08-18 10:46:12 -04002054 // TODO(jmadill): Investigate implementation limits on D3D11
2055 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04002056 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057 componentCount > caps.maxTransformFeedbackSeparateComponents)
2058 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002059 infoLog << "Transform feedback varying's " << varying->name << " components ("
2060 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002061 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002062 return false;
2063 }
2064
2065 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002066 found = true;
2067 break;
2068 }
2069 }
2070
Jamie Madill89bb70e2015-08-31 14:18:39 -04002071 // TODO(jmadill): investigate if we can support capturing array elements.
2072 if (tfVaryingName.find('[') != std::string::npos)
2073 {
2074 infoLog << "Capture of array elements not currently supported.";
2075 return false;
2076 }
2077
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2079 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002080 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002081 }
2082
Jamie Madillada9ecc2015-08-17 12:53:37 -04002083 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002084 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085 {
Jamie Madillf6113162015-05-07 11:49:21 -04002086 infoLog << "Transform feedback varying total components (" << totalComponents
2087 << ") exceed the maximum interleaved components ("
2088 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002089 return false;
2090 }
2091
2092 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002093}
2094
Jamie Madillccdf74b2015-08-18 10:46:12 -04002095void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2096{
2097 // Gather the linked varyings that are used for transform feedback, they should all exist.
2098 mData.mTransformFeedbackVaryingVars.clear();
2099 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2100 {
2101 for (const sh::Varying *varying : varyings)
2102 {
2103 if (tfVaryingName == varying->name)
2104 {
2105 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2106 break;
2107 }
2108 }
2109 }
2110}
2111
2112std::vector<const sh::Varying *> Program::getMergedVaryings() const
2113{
2114 std::set<std::string> uniqueNames;
2115 std::vector<const sh::Varying *> varyings;
2116
2117 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2118 {
2119 if (uniqueNames.count(varying.name) == 0)
2120 {
2121 uniqueNames.insert(varying.name);
2122 varyings.push_back(&varying);
2123 }
2124 }
2125
2126 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2127 {
2128 if (uniqueNames.count(varying.name) == 0)
2129 {
2130 uniqueNames.insert(varying.name);
2131 varyings.push_back(&varying);
2132 }
2133 }
2134
2135 return varyings;
2136}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002137
2138void Program::linkOutputVariables()
2139{
2140 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2141 ASSERT(fragmentShader != nullptr);
2142
2143 // Skip this step for GLES2 shaders.
2144 if (fragmentShader->getShaderVersion() == 100)
2145 return;
2146
Jamie Madilla0a9e122015-09-02 15:54:30 -04002147 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002148
2149 // TODO(jmadill): any caps validation here?
2150
2151 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2152 outputVariableIndex++)
2153 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002154 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002155
2156 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2157 if (outputVariable.isBuiltIn())
2158 continue;
2159
2160 // Since multiple output locations must be specified, use 0 for non-specified locations.
2161 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2162
2163 ASSERT(outputVariable.staticUse);
2164
2165 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2166 elementIndex++)
2167 {
2168 const int location = baseLocation + elementIndex;
2169 ASSERT(mData.mOutputVariables.count(location) == 0);
2170 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2171 mData.mOutputVariables[location] =
2172 VariableLocation(outputVariable.name, element, outputVariableIndex);
2173 }
2174 }
2175}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002176
2177bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2178{
2179 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2180 VectorAndSamplerCount vsCounts;
2181
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002182 std::vector<LinkedUniform> samplerUniforms;
2183
Jamie Madill62d31cb2015-09-11 13:25:51 -04002184 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2185 {
2186 if (uniform.staticUse)
2187 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002188 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002189 }
2190 }
2191
2192 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2193 {
2194 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2195 << caps.maxVertexUniformVectors << ").";
2196 return false;
2197 }
2198
2199 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2200 {
2201 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2202 << caps.maxVertexTextureImageUnits << ").";
2203 return false;
2204 }
2205
2206 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2207 VectorAndSamplerCount fsCounts;
2208
2209 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2210 {
2211 if (uniform.staticUse)
2212 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002213 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002214 }
2215 }
2216
2217 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2218 {
2219 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2220 << caps.maxFragmentUniformVectors << ").";
2221 return false;
2222 }
2223
2224 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2225 {
2226 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2227 << caps.maxTextureImageUnits << ").";
2228 return false;
2229 }
2230
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002231 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2232 mSamplerUniformRange.end =
2233 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2234
2235 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2236
Jamie Madill62d31cb2015-09-11 13:25:51 -04002237 return true;
2238}
2239
2240Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002241 const std::string &fullName,
2242 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002243{
2244 VectorAndSamplerCount vectorAndSamplerCount;
2245
2246 if (uniform.isStruct())
2247 {
2248 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2249 {
2250 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2251
2252 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2253 {
2254 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2255 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2256
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002257 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002258 }
2259 }
2260
2261 return vectorAndSamplerCount;
2262 }
2263
2264 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002265 bool isSampler = IsSamplerType(uniform.type);
2266 if (!UniformInList(mData.getUniforms(), fullName) && !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002267 {
2268 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2269 uniform.arraySize, -1,
2270 sh::BlockMemberInfo::getDefaultBlockInfo());
2271 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002272
2273 // Store sampler uniforms separately, so we'll append them to the end of the list.
2274 if (isSampler)
2275 {
2276 samplerUniforms->push_back(linkedUniform);
2277 }
2278 else
2279 {
2280 mData.mUniforms.push_back(linkedUniform);
2281 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002282 }
2283
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002284 unsigned int elementCount = uniform.elementCount();
2285 vectorAndSamplerCount.vectorCount = (VariableRegisterCount(uniform.type) * elementCount);
2286 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002287
2288 return vectorAndSamplerCount;
2289}
2290
2291void Program::gatherInterfaceBlockInfo()
2292{
2293 std::set<std::string> visitedList;
2294
2295 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2296
2297 ASSERT(mData.mUniformBlocks.empty());
2298 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2299 {
2300 // Only 'packed' blocks are allowed to be considered inacive.
2301 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2302 continue;
2303
2304 if (visitedList.count(vertexBlock.name) > 0)
2305 continue;
2306
2307 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2308 visitedList.insert(vertexBlock.name);
2309 }
2310
2311 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2312
2313 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2314 {
2315 // Only 'packed' blocks are allowed to be considered inacive.
2316 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2317 continue;
2318
2319 if (visitedList.count(fragmentBlock.name) > 0)
2320 {
2321 for (gl::UniformBlock &block : mData.mUniformBlocks)
2322 {
2323 if (block.name == fragmentBlock.name)
2324 {
2325 block.fragmentStaticUse = fragmentBlock.staticUse;
2326 }
2327 }
2328
2329 continue;
2330 }
2331
2332 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2333 visitedList.insert(fragmentBlock.name);
2334 }
2335}
2336
2337void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2338{
2339 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2340 size_t firstBlockUniformIndex = mData.mUniforms.size();
2341 DefineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, &mData.mUniforms);
2342 size_t lastBlockUniformIndex = mData.mUniforms.size();
2343
2344 std::vector<unsigned int> blockUniformIndexes;
2345 for (size_t blockUniformIndex = firstBlockUniformIndex;
2346 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2347 {
2348 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2349 }
2350
2351 if (interfaceBlock.arraySize > 0)
2352 {
2353 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2354 {
2355 UniformBlock block(interfaceBlock.name, true, arrayElement);
2356 block.memberUniformIndexes = blockUniformIndexes;
2357
2358 if (shaderType == GL_VERTEX_SHADER)
2359 {
2360 block.vertexStaticUse = interfaceBlock.staticUse;
2361 }
2362 else
2363 {
2364 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2365 block.fragmentStaticUse = interfaceBlock.staticUse;
2366 }
2367
2368 mData.mUniformBlocks.push_back(block);
2369 }
2370 }
2371 else
2372 {
2373 UniformBlock block(interfaceBlock.name, false, 0);
2374 block.memberUniformIndexes = blockUniformIndexes;
2375
2376 if (shaderType == GL_VERTEX_SHADER)
2377 {
2378 block.vertexStaticUse = interfaceBlock.staticUse;
2379 }
2380 else
2381 {
2382 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2383 block.fragmentStaticUse = interfaceBlock.staticUse;
2384 }
2385
2386 mData.mUniformBlocks.push_back(block);
2387 }
2388}
2389
2390template <typename T>
2391void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2392{
2393 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2394 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2395 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2396
2397 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2398 {
2399 // Do a cast conversion for boolean types. From the spec:
2400 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2401 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2402 for (GLsizei component = 0; component < count; ++component)
2403 {
2404 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2405 }
2406 }
2407 else
2408 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002409 // Invalide the validation cache if we modify the sampler data.
2410 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2411 {
2412 mCachedValidateSamplersResult.reset();
2413 }
2414
Jamie Madill62d31cb2015-09-11 13:25:51 -04002415 memcpy(destPointer, v, sizeof(T) * count);
2416 }
2417}
2418
2419template <size_t cols, size_t rows, typename T>
2420void Program::setMatrixUniformInternal(GLint location,
2421 GLsizei count,
2422 GLboolean transpose,
2423 const T *v)
2424{
2425 if (!transpose)
2426 {
2427 setUniformInternal(location, count * cols * rows, v);
2428 return;
2429 }
2430
2431 // Perform a transposing copy.
2432 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2433 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2434 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2435 for (GLsizei element = 0; element < count; ++element)
2436 {
2437 size_t elementOffset = element * rows * cols;
2438
2439 for (size_t row = 0; row < rows; ++row)
2440 {
2441 for (size_t col = 0; col < cols; ++col)
2442 {
2443 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2444 }
2445 }
2446 }
2447}
2448
2449template <typename DestT>
2450void Program::getUniformInternal(GLint location, DestT *dataOut) const
2451{
2452 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2453 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2454
2455 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2456
2457 GLenum componentType = VariableComponentType(uniform.type);
2458 if (componentType == GLTypeToGLenum<DestT>::value)
2459 {
2460 memcpy(dataOut, srcPointer, uniform.getElementSize());
2461 return;
2462 }
2463
2464 int components = VariableComponentCount(uniform.type) * uniform.elementCount();
2465
2466 switch (componentType)
2467 {
2468 case GL_INT:
2469 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2470 break;
2471 case GL_UNSIGNED_INT:
2472 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2473 break;
2474 case GL_BOOL:
2475 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2476 break;
2477 case GL_FLOAT:
2478 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2479 break;
2480 default:
2481 UNREACHABLE();
2482 }
2483}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484}