blob: 48a88537cdde96137c26612bd7bc400ea9ca1ef7 [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
212void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog)
213{
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
574bool Program::isLinked()
575{
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
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000864void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
865{
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
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000869void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders)
870{
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 Lang7dd2e102014-11-10 15:19:26 -0500899GLuint Program::getAttributeLocation(const std::string &name)
900{
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
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000973GLint Program::getActiveAttributeCount()
974{
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
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000990GLint Program::getActiveAttributeMaxLength()
991{
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
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001025void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1026{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001027 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001028 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001029 // index must be smaller than getActiveUniformCount()
1030 ASSERT(index < mData.mUniforms.size());
1031 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001032
1033 if (bufsize > 0)
1034 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001035 std::string string = uniform.name;
1036 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001037 {
1038 string += "[0]";
1039 }
1040
1041 strncpy(name, string.c_str(), bufsize);
1042 name[bufsize - 1] = '\0';
1043
1044 if (length)
1045 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001046 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001047 }
1048 }
1049
Jamie Madill62d31cb2015-09-11 13:25:51 -04001050 *size = uniform.elementCount();
1051 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001052 }
1053 else
1054 {
1055 if (bufsize > 0)
1056 {
1057 name[0] = '\0';
1058 }
1059
1060 if (length)
1061 {
1062 *length = 0;
1063 }
1064
1065 *size = 0;
1066 *type = GL_NONE;
1067 }
1068}
1069
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001070GLint Program::getActiveUniformCount()
1071{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001072 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001073 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001074 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001075 }
1076 else
1077 {
1078 return 0;
1079 }
1080}
1081
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001082GLint Program::getActiveUniformMaxLength()
1083{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001084 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001085
1086 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001087 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001088 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001089 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001090 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001091 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001092 size_t length = uniform.name.length() + 1u;
1093 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001094 {
1095 length += 3; // Counting in "[0]".
1096 }
1097 maxLength = std::max(length, maxLength);
1098 }
1099 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001100 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001101
Jamie Madill62d31cb2015-09-11 13:25:51 -04001102 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001103}
1104
1105GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1106{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001107 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1108 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001109 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001110 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001111 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1112 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1113 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1114 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1115 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1116 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1117 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1118 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1119 default:
1120 UNREACHABLE();
1121 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001122 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001123 return 0;
1124}
1125
1126bool Program::isValidUniformLocation(GLint location) const
1127{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001128 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
1129 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001130}
1131
Jamie Madill62d31cb2015-09-11 13:25:51 -04001132const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001133{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001134 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1135 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001136}
1137
Jamie Madill62d31cb2015-09-11 13:25:51 -04001138GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001139{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001140 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001141}
1142
Jamie Madill62d31cb2015-09-11 13:25:51 -04001143GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001145 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001146}
1147
1148void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1149{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001150 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001151 mProgram->setUniform1fv(location, count, v);
1152}
1153
1154void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1155{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001156 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001157 mProgram->setUniform2fv(location, count, v);
1158}
1159
1160void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1161{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001162 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001163 mProgram->setUniform3fv(location, count, v);
1164}
1165
1166void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1167{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001168 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169 mProgram->setUniform4fv(location, count, v);
1170}
1171
1172void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1173{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001174 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001175 mProgram->setUniform1iv(location, count, v);
1176}
1177
1178void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1179{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001180 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001181 mProgram->setUniform2iv(location, count, v);
1182}
1183
1184void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1185{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001186 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001187 mProgram->setUniform3iv(location, count, v);
1188}
1189
1190void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1191{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001192 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001193 mProgram->setUniform4iv(location, count, v);
1194}
1195
1196void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1197{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001198 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001199 mProgram->setUniform1uiv(location, count, v);
1200}
1201
1202void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1203{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001204 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001205 mProgram->setUniform2uiv(location, count, v);
1206}
1207
1208void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1209{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001210 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211 mProgram->setUniform3uiv(location, count, v);
1212}
1213
1214void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1215{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001216 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001217 mProgram->setUniform4uiv(location, count, v);
1218}
1219
1220void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1221{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001222 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1224}
1225
1226void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1227{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001228 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001229 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1230}
1231
1232void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1233{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001234 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001235 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1236}
1237
1238void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1239{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001240 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001241 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1242}
1243
1244void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1245{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001246 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001247 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1248}
1249
1250void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1251{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001252 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1254}
1255
1256void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1257{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001258 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001259 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1260}
1261
1262void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1263{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001264 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001265 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1266}
1267
1268void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1269{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001270 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1272}
1273
1274void Program::getUniformfv(GLint location, GLfloat *v)
1275{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001276 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001277}
1278
1279void Program::getUniformiv(GLint location, GLint *v)
1280{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001281 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001282}
1283
1284void Program::getUniformuiv(GLint location, GLuint *v)
1285{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001286 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001287}
1288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289void Program::flagForDeletion()
1290{
1291 mDeleteStatus = true;
1292}
1293
1294bool Program::isFlaggedForDeletion() const
1295{
1296 return mDeleteStatus;
1297}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001298
Brandon Jones43a53e22014-08-28 16:23:22 -07001299void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001300{
1301 mInfoLog.reset();
1302
Geoff Lang7dd2e102014-11-10 15:19:26 -05001303 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001304 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001305 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001306 }
1307 else
1308 {
Jamie Madillf6113162015-05-07 11:49:21 -04001309 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001310 }
1311}
1312
Geoff Lang7dd2e102014-11-10 15:19:26 -05001313bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1314{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001315 // Skip cache if we're using an infolog, so we get the full error.
1316 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1317 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1318 {
1319 return mCachedValidateSamplersResult.value();
1320 }
1321
1322 if (mTextureUnitTypesCache.empty())
1323 {
1324 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1325 }
1326 else
1327 {
1328 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1329 }
1330
1331 // if any two active samplers in a program are of different types, but refer to the same
1332 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1333 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1334 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1335 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1336 {
1337 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1338 ASSERT(uniform.isSampler());
1339
1340 if (!uniform.staticUse)
1341 continue;
1342
1343 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1344 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1345
1346 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1347 {
1348 GLuint textureUnit = dataPtr[arrayElement];
1349
1350 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1351 {
1352 if (infoLog)
1353 {
1354 (*infoLog) << "Sampler uniform (" << textureUnit
1355 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1356 << caps.maxCombinedTextureImageUnits << ")";
1357 }
1358
1359 mCachedValidateSamplersResult = false;
1360 return false;
1361 }
1362
1363 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1364 {
1365 if (textureType != mTextureUnitTypesCache[textureUnit])
1366 {
1367 if (infoLog)
1368 {
1369 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1370 "image unit ("
1371 << textureUnit << ").";
1372 }
1373
1374 mCachedValidateSamplersResult = false;
1375 return false;
1376 }
1377 }
1378 else
1379 {
1380 mTextureUnitTypesCache[textureUnit] = textureType;
1381 }
1382 }
1383 }
1384
1385 mCachedValidateSamplersResult = true;
1386 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001387}
1388
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001389bool Program::isValidated() const
1390{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391 return mValidated;
1392}
1393
Geoff Lang7dd2e102014-11-10 15:19:26 -05001394GLuint Program::getActiveUniformBlockCount()
1395{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001396 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397}
1398
1399void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1400{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001401 ASSERT(uniformBlockIndex <
1402 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403
Jamie Madill62d31cb2015-09-11 13:25:51 -04001404 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405
1406 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001407 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408 std::string string = uniformBlock.name;
1409
Jamie Madill62d31cb2015-09-11 13:25:51 -04001410 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001411 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001412 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413 }
1414
1415 strncpy(uniformBlockName, string.c_str(), bufSize);
1416 uniformBlockName[bufSize - 1] = '\0';
1417
1418 if (length)
1419 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001420 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001422 }
1423}
1424
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001426{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001427 ASSERT(uniformBlockIndex <
1428 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429
Jamie Madill62d31cb2015-09-11 13:25:51 -04001430 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431
1432 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001433 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434 case GL_UNIFORM_BLOCK_DATA_SIZE:
1435 *params = static_cast<GLint>(uniformBlock.dataSize);
1436 break;
1437 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001438 *params =
1439 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001440 break;
1441 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1442 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1443 break;
1444 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1445 {
1446 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1447 {
1448 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1449 }
1450 }
1451 break;
1452 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001453 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001454 break;
1455 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001456 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001457 break;
1458 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001459 }
1460}
1461
1462GLint Program::getActiveUniformBlockMaxLength()
1463{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001464 int maxLength = 0;
1465
1466 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001467 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001468 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1470 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001471 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001472 if (!uniformBlock.name.empty())
1473 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001474 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475
1476 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001477 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478
1479 maxLength = std::max(length + arrayLength, maxLength);
1480 }
1481 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001482 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483
1484 return maxLength;
1485}
1486
1487GLuint Program::getUniformBlockIndex(const std::string &name)
1488{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001489 size_t subscript = GL_INVALID_INDEX;
1490 std::string baseName = gl::ParseUniformName(name, &subscript);
1491
1492 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1493 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1494 {
1495 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1496 if (uniformBlock.name == baseName)
1497 {
1498 const bool arrayElementZero =
1499 (subscript == GL_INVALID_INDEX &&
1500 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1501 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1502 {
1503 return blockIndex;
1504 }
1505 }
1506 }
1507
1508 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001509}
1510
Jamie Madill62d31cb2015-09-11 13:25:51 -04001511const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001512{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001513 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1514 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001515}
1516
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001517void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1518{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001519 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001520 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001521}
1522
1523GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1524{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001525 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001526}
1527
1528void Program::resetUniformBlockBindings()
1529{
1530 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1531 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001532 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001533 }
Geoff Lang5d124a62015-09-15 13:03:27 -04001534 mData.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001535}
1536
Geoff Lang48dcae72014-02-05 16:28:24 -05001537void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1538{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001539 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001540 for (GLsizei i = 0; i < count; i++)
1541 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001542 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001543 }
1544
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001545 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001546}
1547
1548void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1549{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001550 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001551 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001552 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1553 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001554 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1555 if (length)
1556 {
1557 *length = lastNameIdx;
1558 }
1559 if (size)
1560 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001561 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001562 }
1563 if (type)
1564 {
1565 *type = varying.type;
1566 }
1567 if (name)
1568 {
1569 memcpy(name, varying.name.c_str(), lastNameIdx);
1570 name[lastNameIdx] = '\0';
1571 }
1572 }
1573}
1574
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001575GLsizei Program::getTransformFeedbackVaryingCount() const
1576{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001577 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001578 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001579 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001580 }
1581 else
1582 {
1583 return 0;
1584 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001585}
1586
1587GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1588{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001589 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001590 {
1591 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001592 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001593 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001594 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1595 }
1596
1597 return maxSize;
1598 }
1599 else
1600 {
1601 return 0;
1602 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001603}
1604
1605GLenum Program::getTransformFeedbackBufferMode() const
1606{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001607 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001608}
1609
Jamie Madillada9ecc2015-08-17 12:53:37 -04001610// static
1611bool Program::linkVaryings(InfoLog &infoLog,
1612 const Shader *vertexShader,
1613 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001614{
Jamie Madill4cff2472015-08-21 16:53:18 -04001615 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1616 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001617
Jamie Madill4cff2472015-08-21 16:53:18 -04001618 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001619 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620 bool matched = false;
1621
1622 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001623 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001624 {
1625 continue;
1626 }
1627
Jamie Madill4cff2472015-08-21 16:53:18 -04001628 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001629 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001630 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001631 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001632 ASSERT(!input.isBuiltIn());
1633 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001634 {
1635 return false;
1636 }
1637
Geoff Lang7dd2e102014-11-10 15:19:26 -05001638 matched = true;
1639 break;
1640 }
1641 }
1642
1643 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001644 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001645 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001646 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001647 return false;
1648 }
1649 }
1650
Jamie Madillada9ecc2015-08-17 12:53:37 -04001651 // TODO(jmadill): verify no unmatched vertex varyings?
1652
Geoff Lang7dd2e102014-11-10 15:19:26 -05001653 return true;
1654}
1655
Jamie Madill62d31cb2015-09-11 13:25:51 -04001656bool Program::linkUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Jamie Madillea918db2015-08-18 14:48:59 -04001657{
1658 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1659 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1660
1661 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001662 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001663
1664 for (const sh::Uniform &vertexUniform : vertexUniforms)
1665 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001666 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001667 }
1668
1669 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1670 {
1671 auto entry = linkedUniforms.find(fragmentUniform.name);
1672 if (entry != linkedUniforms.end())
1673 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001674 LinkedUniform *vertexUniform = &entry->second;
1675 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1676 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001677 {
1678 return false;
1679 }
1680 }
1681 }
1682
Jamie Madill62d31cb2015-09-11 13:25:51 -04001683 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1684 // Also check the maximum uniform vector and sampler counts.
1685 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1686 {
1687 return false;
1688 }
1689
1690 indexUniforms();
1691
Jamie Madillea918db2015-08-18 14:48:59 -04001692 return true;
1693}
1694
Jamie Madill62d31cb2015-09-11 13:25:51 -04001695void Program::indexUniforms()
1696{
1697 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1698 {
1699 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1700
1701 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1702 {
1703 if (!uniform.isBuiltIn())
1704 {
1705 // Assign in-order uniform locations
1706 mData.mUniformLocations.push_back(gl::VariableLocation(
1707 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)));
1708 }
1709 }
1710 }
1711}
1712
Geoff Lang7dd2e102014-11-10 15:19:26 -05001713bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1714{
1715 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
1716 {
1717 return false;
1718 }
1719
1720 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1721 {
Jamie Madillf6113162015-05-07 11:49:21 -04001722 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001723 return false;
1724 }
1725
1726 return true;
1727}
1728
1729// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001730bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001731 InfoLog &infoLog,
1732 const AttributeBindings &attributeBindings,
1733 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001734{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001735 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001736 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001737 GLuint maxAttribs = data.caps->maxVertexAttributes;
1738
1739 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001740 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001741 {
Jamie Madillf6113162015-05-07 11:49:21 -04001742 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001743 return false;
1744 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001745
Jamie Madillc349ec02015-08-21 16:53:12 -04001746 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001747
Jamie Madillc349ec02015-08-21 16:53:12 -04001748 // Link attributes that have a binding location
1749 for (sh::Attribute &attribute : mData.mAttributes)
1750 {
1751 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001752 ASSERT(attribute.staticUse);
1753
Jamie Madillc349ec02015-08-21 16:53:12 -04001754 int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
1755 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001756 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001757 attribute.location = bindingLocation;
1758 }
1759
1760 if (attribute.location != -1)
1761 {
1762 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001763 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001764
Jamie Madill63805b42015-08-25 13:17:39 -04001765 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001766 {
Jamie Madillf6113162015-05-07 11:49:21 -04001767 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001768 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001769
1770 return false;
1771 }
1772
Jamie Madill63805b42015-08-25 13:17:39 -04001773 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001774 {
Jamie Madill63805b42015-08-25 13:17:39 -04001775 const int regLocation = attribute.location + reg;
1776 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777
1778 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001779 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001780 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001781 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001782 // TODO(jmadill): fix aliasing on ES2
1783 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001784 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001785 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001786 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001787 return false;
1788 }
1789 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001790 else
1791 {
Jamie Madill63805b42015-08-25 13:17:39 -04001792 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001793 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001794
Jamie Madill63805b42015-08-25 13:17:39 -04001795 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001796 }
1797 }
1798 }
1799
1800 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001801 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803 ASSERT(attribute.staticUse);
1804
Jamie Madillc349ec02015-08-21 16:53:12 -04001805 // Not set by glBindAttribLocation or by location layout qualifier
1806 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807 {
Jamie Madill63805b42015-08-25 13:17:39 -04001808 int regs = VariableRegisterCount(attribute.type);
1809 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001810
Jamie Madill63805b42015-08-25 13:17:39 -04001811 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001812 {
Jamie Madillf6113162015-05-07 11:49:21 -04001813 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001814 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001815 }
1816
Jamie Madillc349ec02015-08-21 16:53:12 -04001817 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001818 }
1819 }
1820
Jamie Madillc349ec02015-08-21 16:53:12 -04001821 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001822 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001823 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001824 ASSERT(attribute.location != -1);
1825 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001826
Jamie Madill63805b42015-08-25 13:17:39 -04001827 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001828 {
Jamie Madill63805b42015-08-25 13:17:39 -04001829 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830 }
1831 }
1832
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833 return true;
1834}
1835
Jamie Madille473dee2015-08-18 14:49:01 -04001836bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837{
Jamie Madille473dee2015-08-18 14:49:01 -04001838 const Shader &vertexShader = *mData.mAttachedVertexShader;
1839 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1840
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1842 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001843
Geoff Lang7dd2e102014-11-10 15:19:26 -05001844 // Check that interface blocks defined in the vertex and fragment shaders are identical
1845 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1846 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001847
1848 GLuint vertexBlockCount = 0;
1849 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001850 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001852
1853 // Note: shared and std140 layouts are always considered active
1854 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1855 {
1856 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1857 {
1858 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1859 << caps.maxVertexUniformBlocks << ")";
1860 return false;
1861 }
1862 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001863 }
Jamie Madille473dee2015-08-18 14:49:01 -04001864
1865 GLuint fragmentBlockCount = 0;
1866 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001867 {
Jamie Madille473dee2015-08-18 14:49:01 -04001868 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001869 if (entry != linkedUniformBlocks.end())
1870 {
1871 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1872 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1873 {
1874 return false;
1875 }
1876 }
Jamie Madille473dee2015-08-18 14:49:01 -04001877
Geoff Lang7dd2e102014-11-10 15:19:26 -05001878 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001879 if (fragmentInterfaceBlock.staticUse ||
1880 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001881 {
Jamie Madille473dee2015-08-18 14:49:01 -04001882 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001883 {
Jamie Madille473dee2015-08-18 14:49:01 -04001884 infoLog
1885 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1886 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001887 return false;
1888 }
1889 }
1890 }
Jamie Madille473dee2015-08-18 14:49:01 -04001891
Jamie Madill62d31cb2015-09-11 13:25:51 -04001892 gatherInterfaceBlockInfo();
1893
Geoff Lang7dd2e102014-11-10 15:19:26 -05001894 return true;
1895}
1896
1897bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1898 const sh::InterfaceBlock &fragmentInterfaceBlock)
1899{
1900 const char* blockName = vertexInterfaceBlock.name.c_str();
1901 // validate blocks for the same member types
1902 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1903 {
Jamie Madillf6113162015-05-07 11:49:21 -04001904 infoLog << "Types for interface block '" << blockName
1905 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001906 return false;
1907 }
1908 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1909 {
Jamie Madillf6113162015-05-07 11:49:21 -04001910 infoLog << "Array sizes differ for interface block '" << blockName
1911 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912 return false;
1913 }
1914 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
1915 {
Jamie Madillf6113162015-05-07 11:49:21 -04001916 infoLog << "Layout qualifiers differ for interface block '" << blockName
1917 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918 return false;
1919 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001920 const unsigned int numBlockMembers =
1921 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001922 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1923 {
1924 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
1925 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
1926 if (vertexMember.name != fragmentMember.name)
1927 {
Jamie Madillf6113162015-05-07 11:49:21 -04001928 infoLog << "Name mismatch for field " << blockMemberIndex
1929 << " of interface block '" << blockName
1930 << "': (in vertex: '" << vertexMember.name
1931 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001932 return false;
1933 }
1934 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
1935 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
1936 {
1937 return false;
1938 }
1939 }
1940 return true;
1941}
1942
1943bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
1944 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
1945{
1946 if (vertexVariable.type != fragmentVariable.type)
1947 {
Jamie Madillf6113162015-05-07 11:49:21 -04001948 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001949 return false;
1950 }
1951 if (vertexVariable.arraySize != fragmentVariable.arraySize)
1952 {
Jamie Madillf6113162015-05-07 11:49:21 -04001953 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 return false;
1955 }
1956 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
1957 {
Jamie Madillf6113162015-05-07 11:49:21 -04001958 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959 return false;
1960 }
1961
1962 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
1963 {
Jamie Madillf6113162015-05-07 11:49:21 -04001964 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001965 return false;
1966 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001967 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1969 {
1970 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
1971 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
1972
1973 if (vertexMember.name != fragmentMember.name)
1974 {
Jamie Madillf6113162015-05-07 11:49:21 -04001975 infoLog << "Name mismatch for field '" << memberIndex
1976 << "' of " << variableName
1977 << ": (in vertex: '" << vertexMember.name
1978 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001979 return false;
1980 }
1981
1982 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
1983 vertexMember.name + "'";
1984
1985 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
1986 {
1987 return false;
1988 }
1989 }
1990
1991 return true;
1992}
1993
1994bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
1995{
Cooper Partin1acf4382015-06-12 12:38:57 -07001996#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
1997 const bool validatePrecision = true;
1998#else
1999 const bool validatePrecision = false;
2000#endif
2001
2002 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003 {
2004 return false;
2005 }
2006
2007 return true;
2008}
2009
2010bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
2011{
2012 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2013 {
2014 return false;
2015 }
2016
Jamie Madille9cc4692015-02-19 16:00:13 -05002017 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018 {
Jamie Madillf6113162015-05-07 11:49:21 -04002019 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002020 return false;
2021 }
2022
2023 return true;
2024}
2025
Jamie Madillccdf74b2015-08-18 10:46:12 -04002026bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2027 const std::vector<const sh::Varying *> &varyings,
2028 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002029{
2030 size_t totalComponents = 0;
2031
Jamie Madillccdf74b2015-08-18 10:46:12 -04002032 std::set<std::string> uniqueNames;
2033
2034 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002035 {
2036 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002037 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002038 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002039 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002041 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002043 infoLog << "Two transform feedback varyings specify the same output variable ("
2044 << tfVaryingName << ").";
2045 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002046 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002047 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002048
Jamie Madillccdf74b2015-08-18 10:46:12 -04002049 // TODO(jmadill): Investigate implementation limits on D3D11
2050 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04002051 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002052 componentCount > caps.maxTransformFeedbackSeparateComponents)
2053 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002054 infoLog << "Transform feedback varying's " << varying->name << " components ("
2055 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002056 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057 return false;
2058 }
2059
2060 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061 found = true;
2062 break;
2063 }
2064 }
2065
Jamie Madill89bb70e2015-08-31 14:18:39 -04002066 // TODO(jmadill): investigate if we can support capturing array elements.
2067 if (tfVaryingName.find('[') != std::string::npos)
2068 {
2069 infoLog << "Capture of array elements not currently supported.";
2070 return false;
2071 }
2072
Geoff Lang7dd2e102014-11-10 15:19:26 -05002073 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2074 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002075 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002076 }
2077
Jamie Madillada9ecc2015-08-17 12:53:37 -04002078 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002079 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002080 {
Jamie Madillf6113162015-05-07 11:49:21 -04002081 infoLog << "Transform feedback varying total components (" << totalComponents
2082 << ") exceed the maximum interleaved components ("
2083 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002084 return false;
2085 }
2086
2087 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002088}
2089
Jamie Madillccdf74b2015-08-18 10:46:12 -04002090void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2091{
2092 // Gather the linked varyings that are used for transform feedback, they should all exist.
2093 mData.mTransformFeedbackVaryingVars.clear();
2094 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2095 {
2096 for (const sh::Varying *varying : varyings)
2097 {
2098 if (tfVaryingName == varying->name)
2099 {
2100 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2101 break;
2102 }
2103 }
2104 }
2105}
2106
2107std::vector<const sh::Varying *> Program::getMergedVaryings() const
2108{
2109 std::set<std::string> uniqueNames;
2110 std::vector<const sh::Varying *> varyings;
2111
2112 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2113 {
2114 if (uniqueNames.count(varying.name) == 0)
2115 {
2116 uniqueNames.insert(varying.name);
2117 varyings.push_back(&varying);
2118 }
2119 }
2120
2121 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2122 {
2123 if (uniqueNames.count(varying.name) == 0)
2124 {
2125 uniqueNames.insert(varying.name);
2126 varyings.push_back(&varying);
2127 }
2128 }
2129
2130 return varyings;
2131}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002132
2133void Program::linkOutputVariables()
2134{
2135 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2136 ASSERT(fragmentShader != nullptr);
2137
2138 // Skip this step for GLES2 shaders.
2139 if (fragmentShader->getShaderVersion() == 100)
2140 return;
2141
Jamie Madilla0a9e122015-09-02 15:54:30 -04002142 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002143
2144 // TODO(jmadill): any caps validation here?
2145
2146 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2147 outputVariableIndex++)
2148 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002149 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002150
2151 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2152 if (outputVariable.isBuiltIn())
2153 continue;
2154
2155 // Since multiple output locations must be specified, use 0 for non-specified locations.
2156 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2157
2158 ASSERT(outputVariable.staticUse);
2159
2160 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2161 elementIndex++)
2162 {
2163 const int location = baseLocation + elementIndex;
2164 ASSERT(mData.mOutputVariables.count(location) == 0);
2165 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2166 mData.mOutputVariables[location] =
2167 VariableLocation(outputVariable.name, element, outputVariableIndex);
2168 }
2169 }
2170}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002171
2172bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2173{
2174 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2175 VectorAndSamplerCount vsCounts;
2176
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002177 std::vector<LinkedUniform> samplerUniforms;
2178
Jamie Madill62d31cb2015-09-11 13:25:51 -04002179 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2180 {
2181 if (uniform.staticUse)
2182 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002183 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002184 }
2185 }
2186
2187 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2188 {
2189 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2190 << caps.maxVertexUniformVectors << ").";
2191 return false;
2192 }
2193
2194 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2195 {
2196 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2197 << caps.maxVertexTextureImageUnits << ").";
2198 return false;
2199 }
2200
2201 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2202 VectorAndSamplerCount fsCounts;
2203
2204 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2205 {
2206 if (uniform.staticUse)
2207 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002208 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002209 }
2210 }
2211
2212 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2213 {
2214 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2215 << caps.maxFragmentUniformVectors << ").";
2216 return false;
2217 }
2218
2219 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2220 {
2221 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2222 << caps.maxTextureImageUnits << ").";
2223 return false;
2224 }
2225
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002226 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2227 mSamplerUniformRange.end =
2228 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2229
2230 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2231
Jamie Madill62d31cb2015-09-11 13:25:51 -04002232 return true;
2233}
2234
2235Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002236 const std::string &fullName,
2237 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002238{
2239 VectorAndSamplerCount vectorAndSamplerCount;
2240
2241 if (uniform.isStruct())
2242 {
2243 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2244 {
2245 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2246
2247 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2248 {
2249 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2250 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2251
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002252 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002253 }
2254 }
2255
2256 return vectorAndSamplerCount;
2257 }
2258
2259 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002260 bool isSampler = IsSamplerType(uniform.type);
2261 if (!UniformInList(mData.getUniforms(), fullName) && !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002262 {
2263 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2264 uniform.arraySize, -1,
2265 sh::BlockMemberInfo::getDefaultBlockInfo());
2266 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002267
2268 // Store sampler uniforms separately, so we'll append them to the end of the list.
2269 if (isSampler)
2270 {
2271 samplerUniforms->push_back(linkedUniform);
2272 }
2273 else
2274 {
2275 mData.mUniforms.push_back(linkedUniform);
2276 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002277 }
2278
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002279 unsigned int elementCount = uniform.elementCount();
2280 vectorAndSamplerCount.vectorCount = (VariableRegisterCount(uniform.type) * elementCount);
2281 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002282
2283 return vectorAndSamplerCount;
2284}
2285
2286void Program::gatherInterfaceBlockInfo()
2287{
2288 std::set<std::string> visitedList;
2289
2290 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2291
2292 ASSERT(mData.mUniformBlocks.empty());
2293 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2294 {
2295 // Only 'packed' blocks are allowed to be considered inacive.
2296 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2297 continue;
2298
2299 if (visitedList.count(vertexBlock.name) > 0)
2300 continue;
2301
2302 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2303 visitedList.insert(vertexBlock.name);
2304 }
2305
2306 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2307
2308 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2309 {
2310 // Only 'packed' blocks are allowed to be considered inacive.
2311 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2312 continue;
2313
2314 if (visitedList.count(fragmentBlock.name) > 0)
2315 {
2316 for (gl::UniformBlock &block : mData.mUniformBlocks)
2317 {
2318 if (block.name == fragmentBlock.name)
2319 {
2320 block.fragmentStaticUse = fragmentBlock.staticUse;
2321 }
2322 }
2323
2324 continue;
2325 }
2326
2327 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2328 visitedList.insert(fragmentBlock.name);
2329 }
2330}
2331
2332void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2333{
2334 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2335 size_t firstBlockUniformIndex = mData.mUniforms.size();
2336 DefineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, &mData.mUniforms);
2337 size_t lastBlockUniformIndex = mData.mUniforms.size();
2338
2339 std::vector<unsigned int> blockUniformIndexes;
2340 for (size_t blockUniformIndex = firstBlockUniformIndex;
2341 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2342 {
2343 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2344 }
2345
2346 if (interfaceBlock.arraySize > 0)
2347 {
2348 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2349 {
2350 UniformBlock block(interfaceBlock.name, true, arrayElement);
2351 block.memberUniformIndexes = blockUniformIndexes;
2352
2353 if (shaderType == GL_VERTEX_SHADER)
2354 {
2355 block.vertexStaticUse = interfaceBlock.staticUse;
2356 }
2357 else
2358 {
2359 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2360 block.fragmentStaticUse = interfaceBlock.staticUse;
2361 }
2362
2363 mData.mUniformBlocks.push_back(block);
2364 }
2365 }
2366 else
2367 {
2368 UniformBlock block(interfaceBlock.name, false, 0);
2369 block.memberUniformIndexes = blockUniformIndexes;
2370
2371 if (shaderType == GL_VERTEX_SHADER)
2372 {
2373 block.vertexStaticUse = interfaceBlock.staticUse;
2374 }
2375 else
2376 {
2377 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2378 block.fragmentStaticUse = interfaceBlock.staticUse;
2379 }
2380
2381 mData.mUniformBlocks.push_back(block);
2382 }
2383}
2384
2385template <typename T>
2386void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2387{
2388 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2389 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2390 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2391
2392 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2393 {
2394 // Do a cast conversion for boolean types. From the spec:
2395 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2396 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2397 for (GLsizei component = 0; component < count; ++component)
2398 {
2399 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2400 }
2401 }
2402 else
2403 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002404 // Invalide the validation cache if we modify the sampler data.
2405 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2406 {
2407 mCachedValidateSamplersResult.reset();
2408 }
2409
Jamie Madill62d31cb2015-09-11 13:25:51 -04002410 memcpy(destPointer, v, sizeof(T) * count);
2411 }
2412}
2413
2414template <size_t cols, size_t rows, typename T>
2415void Program::setMatrixUniformInternal(GLint location,
2416 GLsizei count,
2417 GLboolean transpose,
2418 const T *v)
2419{
2420 if (!transpose)
2421 {
2422 setUniformInternal(location, count * cols * rows, v);
2423 return;
2424 }
2425
2426 // Perform a transposing copy.
2427 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2428 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2429 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2430 for (GLsizei element = 0; element < count; ++element)
2431 {
2432 size_t elementOffset = element * rows * cols;
2433
2434 for (size_t row = 0; row < rows; ++row)
2435 {
2436 for (size_t col = 0; col < cols; ++col)
2437 {
2438 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2439 }
2440 }
2441 }
2442}
2443
2444template <typename DestT>
2445void Program::getUniformInternal(GLint location, DestT *dataOut) const
2446{
2447 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2448 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2449
2450 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2451
2452 GLenum componentType = VariableComponentType(uniform.type);
2453 if (componentType == GLTypeToGLenum<DestT>::value)
2454 {
2455 memcpy(dataOut, srcPointer, uniform.getElementSize());
2456 return;
2457 }
2458
2459 int components = VariableComponentCount(uniform.type) * uniform.elementCount();
2460
2461 switch (componentType)
2462 {
2463 case GL_INT:
2464 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2465 break;
2466 case GL_UNSIGNED_INT:
2467 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2468 break;
2469 case GL_BOOL:
2470 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2471 break;
2472 case GL_FLOAT:
2473 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2474 break;
2475 default:
2476 UNREACHABLE();
2477 }
2478}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479}