blob: 14a50baa201e1409590d31b6a4be664c85fa7e45 [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 Madill6cbf4382015-09-22 19:12:11 -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 Madill6cbf4382015-09-22 19:12:11 -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
586 ASSERT(binaryFormat == mProgram->getBinaryFormat());
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000587
Geoff Lang7dd2e102014-11-10 15:19:26 -0500588 BinaryInputStream stream(binary, length);
589
590 GLenum format = stream.readInt<GLenum>();
591 if (format != mProgram->getBinaryFormat())
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000592 {
Jamie Madillf6113162015-05-07 11:49:21 -0400593 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500594 return Error(GL_NO_ERROR);
595 }
596
597 int majorVersion = stream.readInt<int>();
598 int minorVersion = stream.readInt<int>();
599 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
600 {
Jamie Madillf6113162015-05-07 11:49:21 -0400601 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500602 return Error(GL_NO_ERROR);
603 }
604
605 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
606 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
607 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
608 {
Jamie Madillf6113162015-05-07 11:49:21 -0400609 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500610 return Error(GL_NO_ERROR);
611 }
612
Jamie Madill63805b42015-08-25 13:17:39 -0400613 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
614 "Too many vertex attribs for mask");
615 mData.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500616
Jamie Madill3da79b72015-04-27 11:09:17 -0400617 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400618 ASSERT(mData.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400619 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
620 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400621 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400622 LoadShaderVar(&stream, &attrib);
623 attrib.location = stream.readInt<int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400624 mData.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400625 }
626
Jamie Madill62d31cb2015-09-11 13:25:51 -0400627 unsigned int uniformCount = stream.readInt<unsigned int>();
628 ASSERT(mData.mUniforms.empty());
629 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
630 {
631 LinkedUniform uniform;
632 LoadShaderVar(&stream, &uniform);
633
634 uniform.blockIndex = stream.readInt<int>();
635 uniform.blockInfo.offset = stream.readInt<int>();
636 uniform.blockInfo.arrayStride = stream.readInt<int>();
637 uniform.blockInfo.matrixStride = stream.readInt<int>();
638 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
639
640 mData.mUniforms.push_back(uniform);
641 }
642
643 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
644 ASSERT(mData.mUniformLocations.empty());
645 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
646 uniformIndexIndex++)
647 {
648 VariableLocation variable;
649 stream.readString(&variable.name);
650 stream.readInt(&variable.element);
651 stream.readInt(&variable.index);
652
653 mData.mUniformLocations.push_back(variable);
654 }
655
656 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
657 ASSERT(mData.mUniformBlocks.empty());
658 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
659 ++uniformBlockIndex)
660 {
661 UniformBlock uniformBlock;
662 stream.readString(&uniformBlock.name);
663 stream.readBool(&uniformBlock.isArray);
664 stream.readInt(&uniformBlock.arrayElement);
665 stream.readInt(&uniformBlock.dataSize);
666 stream.readBool(&uniformBlock.vertexStaticUse);
667 stream.readBool(&uniformBlock.fragmentStaticUse);
668
669 unsigned int numMembers = stream.readInt<unsigned int>();
670 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
671 {
672 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
673 }
674
675 // TODO(jmadill): Make D3D-only
676 stream.readInt(&uniformBlock.psRegisterIndex);
677 stream.readInt(&uniformBlock.vsRegisterIndex);
678
679 mData.mUniformBlocks.push_back(uniformBlock);
680 }
681
Jamie Madillada9ecc2015-08-17 12:53:37 -0400682 stream.readInt(&mData.mTransformFeedbackBufferMode);
683
Jamie Madill80a6fc02015-08-21 16:53:16 -0400684 unsigned int outputVarCount = stream.readInt<unsigned int>();
685 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
686 {
687 int locationIndex = stream.readInt<int>();
688 VariableLocation locationData;
Jamie Madill6cbf4382015-09-22 19:12:11 -0400689 stream.readInt(&locationData.element);
690 stream.readInt(&locationData.index);
691 stream.readString(&locationData.name);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400692 mData.mOutputVariables[locationIndex] = locationData;
693 }
694
Jamie Madill6cbf4382015-09-22 19:12:11 -0400695 stream.readInt(&mSamplerUniformRange.start);
696 stream.readInt(&mSamplerUniformRange.end);
697
Geoff Lang7dd2e102014-11-10 15:19:26 -0500698 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
699 if (result.error.isError() || !result.linkSuccess)
700 {
Geoff Langb543aff2014-09-30 14:52:54 -0400701 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000702 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000703
Geoff Lang7dd2e102014-11-10 15:19:26 -0500704 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400705 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500706#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
707}
708
709Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
710{
711 if (binaryFormat)
712 {
713 *binaryFormat = mProgram->getBinaryFormat();
714 }
715
716 BinaryOutputStream stream;
717
718 stream.writeInt(mProgram->getBinaryFormat());
719 stream.writeInt(ANGLE_MAJOR_VERSION);
720 stream.writeInt(ANGLE_MINOR_VERSION);
721 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
722
Jamie Madill63805b42015-08-25 13:17:39 -0400723 stream.writeInt(mData.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500724
Jamie Madillc349ec02015-08-21 16:53:12 -0400725 stream.writeInt(mData.mAttributes.size());
726 for (const sh::Attribute &attrib : mData.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400727 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400728 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400729 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400730 }
731
732 stream.writeInt(mData.mUniforms.size());
733 for (const gl::LinkedUniform &uniform : mData.mUniforms)
734 {
735 WriteShaderVar(&stream, uniform);
736
737 // FIXME: referenced
738
739 stream.writeInt(uniform.blockIndex);
740 stream.writeInt(uniform.blockInfo.offset);
741 stream.writeInt(uniform.blockInfo.arrayStride);
742 stream.writeInt(uniform.blockInfo.matrixStride);
743 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
744 }
745
746 stream.writeInt(mData.mUniformLocations.size());
747 for (const auto &variable : mData.mUniformLocations)
748 {
749 stream.writeString(variable.name);
750 stream.writeInt(variable.element);
751 stream.writeInt(variable.index);
752 }
753
754 stream.writeInt(mData.mUniformBlocks.size());
755 for (const UniformBlock &uniformBlock : mData.mUniformBlocks)
756 {
757 stream.writeString(uniformBlock.name);
758 stream.writeInt(uniformBlock.isArray);
759 stream.writeInt(uniformBlock.arrayElement);
760 stream.writeInt(uniformBlock.dataSize);
761
762 stream.writeInt(uniformBlock.vertexStaticUse);
763 stream.writeInt(uniformBlock.fragmentStaticUse);
764
765 stream.writeInt(uniformBlock.memberUniformIndexes.size());
766 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
767 {
768 stream.writeInt(memberUniformIndex);
769 }
770
771 // TODO(jmadill): make D3D-only
772 stream.writeInt(uniformBlock.psRegisterIndex);
773 stream.writeInt(uniformBlock.vsRegisterIndex);
Jamie Madill3da79b72015-04-27 11:09:17 -0400774 }
775
Jamie Madillada9ecc2015-08-17 12:53:37 -0400776 stream.writeInt(mData.mTransformFeedbackBufferMode);
777
Jamie Madill80a6fc02015-08-21 16:53:16 -0400778 stream.writeInt(mData.mOutputVariables.size());
779 for (const auto &outputPair : mData.mOutputVariables)
780 {
781 stream.writeInt(outputPair.first);
782 stream.writeInt(outputPair.second.element);
783 stream.writeInt(outputPair.second.index);
784 stream.writeString(outputPair.second.name);
785 }
786
Jamie Madill6cbf4382015-09-22 19:12:11 -0400787 stream.writeInt(mSamplerUniformRange.start);
788 stream.writeInt(mSamplerUniformRange.end);
789
Geoff Lang7dd2e102014-11-10 15:19:26 -0500790 gl::Error error = mProgram->save(&stream);
791 if (error.isError())
792 {
793 return error;
794 }
795
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700796 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500797 const void *streamData = stream.data();
798
799 if (streamLength > bufSize)
800 {
801 if (length)
802 {
803 *length = 0;
804 }
805
806 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
807 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
808 // sizes and then copy it.
809 return Error(GL_INVALID_OPERATION);
810 }
811
812 if (binary)
813 {
814 char *ptr = reinterpret_cast<char*>(binary);
815
816 memcpy(ptr, streamData, streamLength);
817 ptr += streamLength;
818
819 ASSERT(ptr - streamLength == binary);
820 }
821
822 if (length)
823 {
824 *length = streamLength;
825 }
826
827 return Error(GL_NO_ERROR);
828}
829
830GLint Program::getBinaryLength() const
831{
832 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400833 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500834 if (error.isError())
835 {
836 return 0;
837 }
838
839 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000840}
841
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000842void Program::release()
843{
844 mRefCount--;
845
846 if (mRefCount == 0 && mDeleteStatus)
847 {
848 mResourceManager->deleteProgram(mHandle);
849 }
850}
851
852void Program::addRef()
853{
854 mRefCount++;
855}
856
857unsigned int Program::getRefCount() const
858{
859 return mRefCount;
860}
861
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000862int Program::getInfoLogLength() const
863{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400864 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000865}
866
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000867void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog)
868{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000869 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000870}
871
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000872void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders)
873{
874 int total = 0;
875
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400876 if (mData.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000877 {
878 if (total < maxCount)
879 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400880 shaders[total] = mData.mAttachedVertexShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000881 }
882
883 total++;
884 }
885
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400886 if (mData.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000887 {
888 if (total < maxCount)
889 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400890 shaders[total] = mData.mAttachedFragmentShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000891 }
892
893 total++;
894 }
895
896 if (count)
897 {
898 *count = total;
899 }
900}
901
Geoff Lang7dd2e102014-11-10 15:19:26 -0500902GLuint Program::getAttributeLocation(const std::string &name)
903{
Jamie Madillc349ec02015-08-21 16:53:12 -0400904 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500905 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400906 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500907 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400908 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500909 }
910 }
911
Austin Kinrossb8af7232015-03-16 22:33:25 -0700912 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500913}
914
Jamie Madill63805b42015-08-25 13:17:39 -0400915bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400916{
Olli Etuaho401d9fe2015-08-26 10:19:59 +0300917 ASSERT(attribLocation < mData.mActiveAttribLocationsMask.size());
Jamie Madill63805b42015-08-25 13:17:39 -0400918 return mData.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500919}
920
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000921void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
922{
Jamie Madillc349ec02015-08-21 16:53:12 -0400923 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000924 {
925 if (bufsize > 0)
926 {
927 name[0] = '\0';
928 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500929
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000930 if (length)
931 {
932 *length = 0;
933 }
934
935 *type = GL_NONE;
936 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400937 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000938 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400939
940 size_t attributeIndex = 0;
941
942 for (const sh::Attribute &attribute : mData.mAttributes)
943 {
944 // Skip over inactive attributes
945 if (attribute.staticUse)
946 {
947 if (static_cast<size_t>(index) == attributeIndex)
948 {
949 break;
950 }
951 attributeIndex++;
952 }
953 }
954
955 ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
956 const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
957
958 if (bufsize > 0)
959 {
960 const char *string = attrib.name.c_str();
961
962 strncpy(name, string, bufsize);
963 name[bufsize - 1] = '\0';
964
965 if (length)
966 {
967 *length = static_cast<GLsizei>(strlen(name));
968 }
969 }
970
971 // Always a single 'type' instance
972 *size = 1;
973 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000974}
975
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000976GLint Program::getActiveAttributeCount()
977{
Jamie Madillc349ec02015-08-21 16:53:12 -0400978 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400979 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400980 return 0;
981 }
982
983 GLint count = 0;
984
985 for (const sh::Attribute &attrib : mData.mAttributes)
986 {
987 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000988 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500989
990 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000991}
992
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000993GLint Program::getActiveAttributeMaxLength()
994{
Jamie Madillc349ec02015-08-21 16:53:12 -0400995 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400996 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400997 return 0;
998 }
999
1000 size_t maxLength = 0;
1001
1002 for (const sh::Attribute &attrib : mData.mAttributes)
1003 {
1004 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001005 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001006 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001007 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001008 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001009
Jamie Madillc349ec02015-08-21 16:53:12 -04001010 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001011}
1012
Geoff Lang7dd2e102014-11-10 15:19:26 -05001013GLint Program::getFragDataLocation(const std::string &name) const
1014{
1015 std::string baseName(name);
1016 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001017 for (auto outputPair : mData.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001018 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001019 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001020 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1021 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001022 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001023 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001024 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001025 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001026}
1027
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001028void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1029{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001030 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001031 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001032 // index must be smaller than getActiveUniformCount()
1033 ASSERT(index < mData.mUniforms.size());
1034 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001035
1036 if (bufsize > 0)
1037 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001038 std::string string = uniform.name;
1039 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001040 {
1041 string += "[0]";
1042 }
1043
1044 strncpy(name, string.c_str(), bufsize);
1045 name[bufsize - 1] = '\0';
1046
1047 if (length)
1048 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001049 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001050 }
1051 }
1052
Jamie Madill62d31cb2015-09-11 13:25:51 -04001053 *size = uniform.elementCount();
1054 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001055 }
1056 else
1057 {
1058 if (bufsize > 0)
1059 {
1060 name[0] = '\0';
1061 }
1062
1063 if (length)
1064 {
1065 *length = 0;
1066 }
1067
1068 *size = 0;
1069 *type = GL_NONE;
1070 }
1071}
1072
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001073GLint Program::getActiveUniformCount()
1074{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001075 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001076 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001077 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001078 }
1079 else
1080 {
1081 return 0;
1082 }
1083}
1084
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001085GLint Program::getActiveUniformMaxLength()
1086{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001087 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001088
1089 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001090 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001091 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001092 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001093 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001094 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001095 size_t length = uniform.name.length() + 1u;
1096 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097 {
1098 length += 3; // Counting in "[0]".
1099 }
1100 maxLength = std::max(length, maxLength);
1101 }
1102 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001103 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001104
Jamie Madill62d31cb2015-09-11 13:25:51 -04001105 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106}
1107
1108GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1109{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001110 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1111 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001112 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001113 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001114 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1115 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1116 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1117 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1118 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1119 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1120 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1121 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1122 default:
1123 UNREACHABLE();
1124 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001125 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001126 return 0;
1127}
1128
1129bool Program::isValidUniformLocation(GLint location) const
1130{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001131 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
1132 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001133}
1134
Jamie Madill62d31cb2015-09-11 13:25:51 -04001135const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001136{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001137 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1138 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001139}
1140
Jamie Madill62d31cb2015-09-11 13:25:51 -04001141GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001143 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144}
1145
Jamie Madill62d31cb2015-09-11 13:25:51 -04001146GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001148 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001149}
1150
1151void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1152{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001153 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001154 mProgram->setUniform1fv(location, count, v);
1155}
1156
1157void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1158{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001159 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001160 mProgram->setUniform2fv(location, count, v);
1161}
1162
1163void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1164{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001165 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001166 mProgram->setUniform3fv(location, count, v);
1167}
1168
1169void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1170{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001171 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001172 mProgram->setUniform4fv(location, count, v);
1173}
1174
1175void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1176{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001177 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001178 mProgram->setUniform1iv(location, count, v);
1179}
1180
1181void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1182{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001183 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001184 mProgram->setUniform2iv(location, count, v);
1185}
1186
1187void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1188{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001189 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001190 mProgram->setUniform3iv(location, count, v);
1191}
1192
1193void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1194{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001195 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001196 mProgram->setUniform4iv(location, count, v);
1197}
1198
1199void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1200{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001201 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001202 mProgram->setUniform1uiv(location, count, v);
1203}
1204
1205void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1206{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001207 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001208 mProgram->setUniform2uiv(location, count, v);
1209}
1210
1211void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1212{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001213 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001214 mProgram->setUniform3uiv(location, count, v);
1215}
1216
1217void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1218{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001219 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001220 mProgram->setUniform4uiv(location, count, v);
1221}
1222
1223void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1224{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001225 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001226 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1227}
1228
1229void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1230{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001231 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001232 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1233}
1234
1235void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1236{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001237 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001238 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1239}
1240
1241void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1242{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001243 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001244 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1245}
1246
1247void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1248{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001249 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001250 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1251}
1252
1253void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1254{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001255 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001256 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1257}
1258
1259void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1260{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001261 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001262 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1263}
1264
1265void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1266{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001267 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001268 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1269}
1270
1271void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1272{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001273 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001274 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1275}
1276
1277void Program::getUniformfv(GLint location, GLfloat *v)
1278{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001279 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280}
1281
1282void Program::getUniformiv(GLint location, GLint *v)
1283{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001284 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001285}
1286
1287void Program::getUniformuiv(GLint location, GLuint *v)
1288{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001289 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290}
1291
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292void Program::flagForDeletion()
1293{
1294 mDeleteStatus = true;
1295}
1296
1297bool Program::isFlaggedForDeletion() const
1298{
1299 return mDeleteStatus;
1300}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001301
Brandon Jones43a53e22014-08-28 16:23:22 -07001302void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001303{
1304 mInfoLog.reset();
1305
Geoff Lang7dd2e102014-11-10 15:19:26 -05001306 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001307 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001308 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001309 }
1310 else
1311 {
Jamie Madillf6113162015-05-07 11:49:21 -04001312 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001313 }
1314}
1315
Geoff Lang7dd2e102014-11-10 15:19:26 -05001316bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1317{
Jamie Madill6cbf4382015-09-22 19:12:11 -04001318 // Skip cache if we're using an infolog, so we get the full error.
1319 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1320 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1321 {
1322 return mCachedValidateSamplersResult.value();
1323 }
1324
1325 if (mTextureUnitTypesCache.empty())
1326 {
1327 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1328 }
1329 else
1330 {
1331 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1332 }
1333
1334 // if any two active samplers in a program are of different types, but refer to the same
1335 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1336 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1337 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1338 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1339 {
1340 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1341 ASSERT(uniform.isSampler());
1342
1343 if (!uniform.staticUse)
1344 continue;
1345
1346 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1347 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1348
1349 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1350 {
1351 GLuint textureUnit = dataPtr[arrayElement];
1352
1353 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1354 {
1355 if (infoLog)
1356 {
1357 (*infoLog) << "Sampler uniform (" << textureUnit
1358 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1359 << caps.maxCombinedTextureImageUnits << ")";
1360 }
1361
1362 mCachedValidateSamplersResult = false;
1363 return false;
1364 }
1365
1366 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1367 {
1368 if (textureType != mTextureUnitTypesCache[textureUnit])
1369 {
1370 if (infoLog)
1371 {
1372 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1373 "image unit ("
1374 << textureUnit << ").";
1375 }
1376
1377 mCachedValidateSamplersResult = false;
1378 return false;
1379 }
1380 }
1381 else
1382 {
1383 mTextureUnitTypesCache[textureUnit] = textureType;
1384 }
1385 }
1386 }
1387
1388 mCachedValidateSamplersResult = true;
1389 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390}
1391
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001392bool Program::isValidated() const
1393{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001394 return mValidated;
1395}
1396
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397GLuint Program::getActiveUniformBlockCount()
1398{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001399 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001400}
1401
1402void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1403{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001404 ASSERT(uniformBlockIndex <
1405 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001406
Jamie Madill62d31cb2015-09-11 13:25:51 -04001407 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408
1409 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001410 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001411 std::string string = uniformBlock.name;
1412
Jamie Madill62d31cb2015-09-11 13:25:51 -04001413 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001415 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416 }
1417
1418 strncpy(uniformBlockName, string.c_str(), bufSize);
1419 uniformBlockName[bufSize - 1] = '\0';
1420
1421 if (length)
1422 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001423 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001425 }
1426}
1427
Geoff Lang7dd2e102014-11-10 15:19:26 -05001428void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001429{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001430 ASSERT(uniformBlockIndex <
1431 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432
Jamie Madill62d31cb2015-09-11 13:25:51 -04001433 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434
1435 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001436 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437 case GL_UNIFORM_BLOCK_DATA_SIZE:
1438 *params = static_cast<GLint>(uniformBlock.dataSize);
1439 break;
1440 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001441 *params =
1442 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443 break;
1444 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1445 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1446 break;
1447 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1448 {
1449 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1450 {
1451 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1452 }
1453 }
1454 break;
1455 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001456 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001457 break;
1458 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001459 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001460 break;
1461 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001462 }
1463}
1464
1465GLint Program::getActiveUniformBlockMaxLength()
1466{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001467 int maxLength = 0;
1468
1469 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001470 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001471 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001472 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1473 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001474 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475 if (!uniformBlock.name.empty())
1476 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001477 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001478
1479 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001480 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481
1482 maxLength = std::max(length + arrayLength, maxLength);
1483 }
1484 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001485 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486
1487 return maxLength;
1488}
1489
1490GLuint Program::getUniformBlockIndex(const std::string &name)
1491{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001492 size_t subscript = GL_INVALID_INDEX;
1493 std::string baseName = gl::ParseUniformName(name, &subscript);
1494
1495 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1496 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1497 {
1498 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1499 if (uniformBlock.name == baseName)
1500 {
1501 const bool arrayElementZero =
1502 (subscript == GL_INVALID_INDEX &&
1503 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1504 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1505 {
1506 return blockIndex;
1507 }
1508 }
1509 }
1510
1511 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001512}
1513
Jamie Madill62d31cb2015-09-11 13:25:51 -04001514const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001515{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001516 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1517 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001518}
1519
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001520void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1521{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001522 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001523}
1524
1525GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1526{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001527 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001528}
1529
1530void Program::resetUniformBlockBindings()
1531{
1532 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1533 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001534 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001535 }
1536}
1537
Geoff Lang48dcae72014-02-05 16:28:24 -05001538void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1539{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001540 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001541 for (GLsizei i = 0; i < count; i++)
1542 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001543 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001544 }
1545
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001546 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001547}
1548
1549void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1550{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001551 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001552 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001553 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1554 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001555 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1556 if (length)
1557 {
1558 *length = lastNameIdx;
1559 }
1560 if (size)
1561 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001562 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001563 }
1564 if (type)
1565 {
1566 *type = varying.type;
1567 }
1568 if (name)
1569 {
1570 memcpy(name, varying.name.c_str(), lastNameIdx);
1571 name[lastNameIdx] = '\0';
1572 }
1573 }
1574}
1575
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001576GLsizei Program::getTransformFeedbackVaryingCount() const
1577{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001578 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001579 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001580 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001581 }
1582 else
1583 {
1584 return 0;
1585 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001586}
1587
1588GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1589{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001590 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001591 {
1592 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001593 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001594 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001595 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1596 }
1597
1598 return maxSize;
1599 }
1600 else
1601 {
1602 return 0;
1603 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001604}
1605
1606GLenum Program::getTransformFeedbackBufferMode() const
1607{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001608 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001609}
1610
Jamie Madillada9ecc2015-08-17 12:53:37 -04001611// static
1612bool Program::linkVaryings(InfoLog &infoLog,
1613 const Shader *vertexShader,
1614 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001615{
Jamie Madill4cff2472015-08-21 16:53:18 -04001616 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1617 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001618
Jamie Madill4cff2472015-08-21 16:53:18 -04001619 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001621 bool matched = false;
1622
1623 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001624 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625 {
1626 continue;
1627 }
1628
Jamie Madill4cff2472015-08-21 16:53:18 -04001629 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001630 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001631 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001633 ASSERT(!input.isBuiltIn());
1634 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001635 {
1636 return false;
1637 }
1638
Geoff Lang7dd2e102014-11-10 15:19:26 -05001639 matched = true;
1640 break;
1641 }
1642 }
1643
1644 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001645 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001647 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001648 return false;
1649 }
1650 }
1651
Jamie Madillada9ecc2015-08-17 12:53:37 -04001652 // TODO(jmadill): verify no unmatched vertex varyings?
1653
Geoff Lang7dd2e102014-11-10 15:19:26 -05001654 return true;
1655}
1656
Jamie Madill62d31cb2015-09-11 13:25:51 -04001657bool Program::linkUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Jamie Madillea918db2015-08-18 14:48:59 -04001658{
1659 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1660 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1661
1662 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001663 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001664
1665 for (const sh::Uniform &vertexUniform : vertexUniforms)
1666 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001667 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001668 }
1669
1670 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1671 {
1672 auto entry = linkedUniforms.find(fragmentUniform.name);
1673 if (entry != linkedUniforms.end())
1674 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001675 LinkedUniform *vertexUniform = &entry->second;
1676 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1677 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001678 {
1679 return false;
1680 }
1681 }
1682 }
1683
Jamie Madill62d31cb2015-09-11 13:25:51 -04001684 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1685 // Also check the maximum uniform vector and sampler counts.
1686 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1687 {
1688 return false;
1689 }
1690
1691 indexUniforms();
1692
Jamie Madillea918db2015-08-18 14:48:59 -04001693 return true;
1694}
1695
Jamie Madill62d31cb2015-09-11 13:25:51 -04001696void Program::indexUniforms()
1697{
1698 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1699 {
1700 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1701
1702 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1703 {
1704 if (!uniform.isBuiltIn())
1705 {
1706 // Assign in-order uniform locations
1707 mData.mUniformLocations.push_back(gl::VariableLocation(
1708 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)));
1709 }
1710 }
1711 }
1712}
1713
Geoff Lang7dd2e102014-11-10 15:19:26 -05001714bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1715{
1716 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
1717 {
1718 return false;
1719 }
1720
1721 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1722 {
Jamie Madillf6113162015-05-07 11:49:21 -04001723 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001724 return false;
1725 }
1726
1727 return true;
1728}
1729
1730// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001731bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001732 InfoLog &infoLog,
1733 const AttributeBindings &attributeBindings,
1734 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001735{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001736 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001737 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001738 GLuint maxAttribs = data.caps->maxVertexAttributes;
1739
1740 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001741 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001742 {
Jamie Madillf6113162015-05-07 11:49:21 -04001743 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001744 return false;
1745 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001746
Jamie Madillc349ec02015-08-21 16:53:12 -04001747 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001748
Jamie Madillc349ec02015-08-21 16:53:12 -04001749 // Link attributes that have a binding location
1750 for (sh::Attribute &attribute : mData.mAttributes)
1751 {
1752 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001753 ASSERT(attribute.staticUse);
1754
Jamie Madillc349ec02015-08-21 16:53:12 -04001755 int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
1756 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001757 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001758 attribute.location = bindingLocation;
1759 }
1760
1761 if (attribute.location != -1)
1762 {
1763 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001764 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001765
Jamie Madill63805b42015-08-25 13:17:39 -04001766 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001767 {
Jamie Madillf6113162015-05-07 11:49:21 -04001768 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001769 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001770
1771 return false;
1772 }
1773
Jamie Madill63805b42015-08-25 13:17:39 -04001774 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775 {
Jamie Madill63805b42015-08-25 13:17:39 -04001776 const int regLocation = attribute.location + reg;
1777 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001778
1779 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001780 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001781 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001783 // TODO(jmadill): fix aliasing on ES2
1784 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001785 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001786 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001787 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001788 return false;
1789 }
1790 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001791 else
1792 {
Jamie Madill63805b42015-08-25 13:17:39 -04001793 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001794 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795
Jamie Madill63805b42015-08-25 13:17:39 -04001796 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001797 }
1798 }
1799 }
1800
1801 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001802 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001804 ASSERT(attribute.staticUse);
1805
Jamie Madillc349ec02015-08-21 16:53:12 -04001806 // Not set by glBindAttribLocation or by location layout qualifier
1807 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001808 {
Jamie Madill63805b42015-08-25 13:17:39 -04001809 int regs = VariableRegisterCount(attribute.type);
1810 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001811
Jamie Madill63805b42015-08-25 13:17:39 -04001812 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001813 {
Jamie Madillf6113162015-05-07 11:49:21 -04001814 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001815 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001816 }
1817
Jamie Madillc349ec02015-08-21 16:53:12 -04001818 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001819 }
1820 }
1821
Jamie Madillc349ec02015-08-21 16:53:12 -04001822 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001823 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001824 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001825 ASSERT(attribute.location != -1);
1826 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001827
Jamie Madill63805b42015-08-25 13:17:39 -04001828 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829 {
Jamie Madill63805b42015-08-25 13:17:39 -04001830 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831 }
1832 }
1833
Geoff Lang7dd2e102014-11-10 15:19:26 -05001834 return true;
1835}
1836
Jamie Madille473dee2015-08-18 14:49:01 -04001837bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001838{
Jamie Madille473dee2015-08-18 14:49:01 -04001839 const Shader &vertexShader = *mData.mAttachedVertexShader;
1840 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1841
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1843 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001844
Geoff Lang7dd2e102014-11-10 15:19:26 -05001845 // Check that interface blocks defined in the vertex and fragment shaders are identical
1846 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1847 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001848
1849 GLuint vertexBlockCount = 0;
1850 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001852 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001853
1854 // Note: shared and std140 layouts are always considered active
1855 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1856 {
1857 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1858 {
1859 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1860 << caps.maxVertexUniformBlocks << ")";
1861 return false;
1862 }
1863 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001864 }
Jamie Madille473dee2015-08-18 14:49:01 -04001865
1866 GLuint fragmentBlockCount = 0;
1867 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 {
Jamie Madille473dee2015-08-18 14:49:01 -04001869 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001870 if (entry != linkedUniformBlocks.end())
1871 {
1872 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1873 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1874 {
1875 return false;
1876 }
1877 }
Jamie Madille473dee2015-08-18 14:49:01 -04001878
Geoff Lang7dd2e102014-11-10 15:19:26 -05001879 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001880 if (fragmentInterfaceBlock.staticUse ||
1881 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001882 {
Jamie Madille473dee2015-08-18 14:49:01 -04001883 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884 {
Jamie Madille473dee2015-08-18 14:49:01 -04001885 infoLog
1886 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1887 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001888 return false;
1889 }
1890 }
1891 }
Jamie Madille473dee2015-08-18 14:49:01 -04001892
Jamie Madill62d31cb2015-09-11 13:25:51 -04001893 gatherInterfaceBlockInfo();
1894
Geoff Lang7dd2e102014-11-10 15:19:26 -05001895 return true;
1896}
1897
1898bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1899 const sh::InterfaceBlock &fragmentInterfaceBlock)
1900{
1901 const char* blockName = vertexInterfaceBlock.name.c_str();
1902 // validate blocks for the same member types
1903 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1904 {
Jamie Madillf6113162015-05-07 11:49:21 -04001905 infoLog << "Types for interface block '" << blockName
1906 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001907 return false;
1908 }
1909 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1910 {
Jamie Madillf6113162015-05-07 11:49:21 -04001911 infoLog << "Array sizes differ for interface block '" << blockName
1912 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001913 return false;
1914 }
1915 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
1916 {
Jamie Madillf6113162015-05-07 11:49:21 -04001917 infoLog << "Layout qualifiers differ for interface block '" << blockName
1918 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001919 return false;
1920 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001921 const unsigned int numBlockMembers =
1922 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1924 {
1925 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
1926 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
1927 if (vertexMember.name != fragmentMember.name)
1928 {
Jamie Madillf6113162015-05-07 11:49:21 -04001929 infoLog << "Name mismatch for field " << blockMemberIndex
1930 << " of interface block '" << blockName
1931 << "': (in vertex: '" << vertexMember.name
1932 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001933 return false;
1934 }
1935 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
1936 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
1937 {
1938 return false;
1939 }
1940 }
1941 return true;
1942}
1943
1944bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
1945 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
1946{
1947 if (vertexVariable.type != fragmentVariable.type)
1948 {
Jamie Madillf6113162015-05-07 11:49:21 -04001949 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001950 return false;
1951 }
1952 if (vertexVariable.arraySize != fragmentVariable.arraySize)
1953 {
Jamie Madillf6113162015-05-07 11:49:21 -04001954 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001955 return false;
1956 }
1957 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
1958 {
Jamie Madillf6113162015-05-07 11:49:21 -04001959 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001960 return false;
1961 }
1962
1963 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
1964 {
Jamie Madillf6113162015-05-07 11:49:21 -04001965 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001966 return false;
1967 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001968 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001969 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1970 {
1971 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
1972 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
1973
1974 if (vertexMember.name != fragmentMember.name)
1975 {
Jamie Madillf6113162015-05-07 11:49:21 -04001976 infoLog << "Name mismatch for field '" << memberIndex
1977 << "' of " << variableName
1978 << ": (in vertex: '" << vertexMember.name
1979 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980 return false;
1981 }
1982
1983 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
1984 vertexMember.name + "'";
1985
1986 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
1987 {
1988 return false;
1989 }
1990 }
1991
1992 return true;
1993}
1994
1995bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
1996{
Cooper Partin1acf4382015-06-12 12:38:57 -07001997#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
1998 const bool validatePrecision = true;
1999#else
2000 const bool validatePrecision = false;
2001#endif
2002
2003 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004 {
2005 return false;
2006 }
2007
2008 return true;
2009}
2010
2011bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
2012{
2013 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2014 {
2015 return false;
2016 }
2017
Jamie Madille9cc4692015-02-19 16:00:13 -05002018 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019 {
Jamie Madillf6113162015-05-07 11:49:21 -04002020 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002021 return false;
2022 }
2023
2024 return true;
2025}
2026
Jamie Madillccdf74b2015-08-18 10:46:12 -04002027bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2028 const std::vector<const sh::Varying *> &varyings,
2029 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002030{
2031 size_t totalComponents = 0;
2032
Jamie Madillccdf74b2015-08-18 10:46:12 -04002033 std::set<std::string> uniqueNames;
2034
2035 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002036 {
2037 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002038 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002040 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002041 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002042 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002044 infoLog << "Two transform feedback varyings specify the same output variable ("
2045 << tfVaryingName << ").";
2046 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002047 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002048 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002049
Jamie Madillccdf74b2015-08-18 10:46:12 -04002050 // TODO(jmadill): Investigate implementation limits on D3D11
2051 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04002052 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 componentCount > caps.maxTransformFeedbackSeparateComponents)
2054 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002055 infoLog << "Transform feedback varying's " << varying->name << " components ("
2056 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002057 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002058 return false;
2059 }
2060
2061 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002062 found = true;
2063 break;
2064 }
2065 }
2066
Jamie Madill89bb70e2015-08-31 14:18:39 -04002067 // TODO(jmadill): investigate if we can support capturing array elements.
2068 if (tfVaryingName.find('[') != std::string::npos)
2069 {
2070 infoLog << "Capture of array elements not currently supported.";
2071 return false;
2072 }
2073
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2075 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002076 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077 }
2078
Jamie Madillada9ecc2015-08-17 12:53:37 -04002079 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002080 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002081 {
Jamie Madillf6113162015-05-07 11:49:21 -04002082 infoLog << "Transform feedback varying total components (" << totalComponents
2083 << ") exceed the maximum interleaved components ("
2084 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085 return false;
2086 }
2087
2088 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002089}
2090
Jamie Madillccdf74b2015-08-18 10:46:12 -04002091void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2092{
2093 // Gather the linked varyings that are used for transform feedback, they should all exist.
2094 mData.mTransformFeedbackVaryingVars.clear();
2095 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2096 {
2097 for (const sh::Varying *varying : varyings)
2098 {
2099 if (tfVaryingName == varying->name)
2100 {
2101 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2102 break;
2103 }
2104 }
2105 }
2106}
2107
2108std::vector<const sh::Varying *> Program::getMergedVaryings() const
2109{
2110 std::set<std::string> uniqueNames;
2111 std::vector<const sh::Varying *> varyings;
2112
2113 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2114 {
2115 if (uniqueNames.count(varying.name) == 0)
2116 {
2117 uniqueNames.insert(varying.name);
2118 varyings.push_back(&varying);
2119 }
2120 }
2121
2122 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2123 {
2124 if (uniqueNames.count(varying.name) == 0)
2125 {
2126 uniqueNames.insert(varying.name);
2127 varyings.push_back(&varying);
2128 }
2129 }
2130
2131 return varyings;
2132}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002133
2134void Program::linkOutputVariables()
2135{
2136 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2137 ASSERT(fragmentShader != nullptr);
2138
2139 // Skip this step for GLES2 shaders.
2140 if (fragmentShader->getShaderVersion() == 100)
2141 return;
2142
Jamie Madilla0a9e122015-09-02 15:54:30 -04002143 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002144
2145 // TODO(jmadill): any caps validation here?
2146
2147 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2148 outputVariableIndex++)
2149 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002150 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002151
2152 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2153 if (outputVariable.isBuiltIn())
2154 continue;
2155
2156 // Since multiple output locations must be specified, use 0 for non-specified locations.
2157 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2158
2159 ASSERT(outputVariable.staticUse);
2160
2161 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2162 elementIndex++)
2163 {
2164 const int location = baseLocation + elementIndex;
2165 ASSERT(mData.mOutputVariables.count(location) == 0);
2166 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2167 mData.mOutputVariables[location] =
2168 VariableLocation(outputVariable.name, element, outputVariableIndex);
2169 }
2170 }
2171}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002172
2173bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2174{
2175 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2176 VectorAndSamplerCount vsCounts;
2177
Jamie Madill6cbf4382015-09-22 19:12:11 -04002178 std::vector<LinkedUniform> samplerUniforms;
2179
Jamie Madill62d31cb2015-09-11 13:25:51 -04002180 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2181 {
2182 if (uniform.staticUse)
2183 {
Jamie Madill6cbf4382015-09-22 19:12:11 -04002184 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002185 }
2186 }
2187
2188 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2189 {
2190 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2191 << caps.maxVertexUniformVectors << ").";
2192 return false;
2193 }
2194
2195 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2196 {
2197 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2198 << caps.maxVertexTextureImageUnits << ").";
2199 return false;
2200 }
2201
2202 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2203 VectorAndSamplerCount fsCounts;
2204
2205 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2206 {
2207 if (uniform.staticUse)
2208 {
Jamie Madill6cbf4382015-09-22 19:12:11 -04002209 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002210 }
2211 }
2212
2213 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2214 {
2215 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2216 << caps.maxFragmentUniformVectors << ").";
2217 return false;
2218 }
2219
2220 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2221 {
2222 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2223 << caps.maxTextureImageUnits << ").";
2224 return false;
2225 }
2226
Jamie Madill6cbf4382015-09-22 19:12:11 -04002227 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2228 mSamplerUniformRange.end =
2229 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2230
2231 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2232
Jamie Madill62d31cb2015-09-11 13:25:51 -04002233 return true;
2234}
2235
2236Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill6cbf4382015-09-22 19:12:11 -04002237 const std::string &fullName,
2238 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002239{
2240 VectorAndSamplerCount vectorAndSamplerCount;
2241
2242 if (uniform.isStruct())
2243 {
2244 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2245 {
2246 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2247
2248 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2249 {
2250 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2251 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2252
Jamie Madill6cbf4382015-09-22 19:12:11 -04002253 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002254 }
2255 }
2256
2257 return vectorAndSamplerCount;
2258 }
2259
2260 // Not a struct
Jamie Madill6cbf4382015-09-22 19:12:11 -04002261 bool isSampler = IsSamplerType(uniform.type);
2262 if (!UniformInList(mData.getUniforms(), fullName) && !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002263 {
2264 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2265 uniform.arraySize, -1,
2266 sh::BlockMemberInfo::getDefaultBlockInfo());
2267 linkedUniform.staticUse = true;
Jamie Madill6cbf4382015-09-22 19:12:11 -04002268
2269 // Store sampler uniforms separately, so we'll append them to the end of the list.
2270 if (isSampler)
2271 {
2272 samplerUniforms->push_back(linkedUniform);
2273 }
2274 else
2275 {
2276 mData.mUniforms.push_back(linkedUniform);
2277 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002278 }
2279
Jamie Madill6cbf4382015-09-22 19:12:11 -04002280 unsigned int elementCount = uniform.elementCount();
2281 vectorAndSamplerCount.vectorCount = (VariableRegisterCount(uniform.type) * elementCount);
2282 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002283
2284 return vectorAndSamplerCount;
2285}
2286
2287void Program::gatherInterfaceBlockInfo()
2288{
2289 std::set<std::string> visitedList;
2290
2291 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2292
2293 ASSERT(mData.mUniformBlocks.empty());
2294 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2295 {
2296 // Only 'packed' blocks are allowed to be considered inacive.
2297 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2298 continue;
2299
2300 if (visitedList.count(vertexBlock.name) > 0)
2301 continue;
2302
2303 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2304 visitedList.insert(vertexBlock.name);
2305 }
2306
2307 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2308
2309 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2310 {
2311 // Only 'packed' blocks are allowed to be considered inacive.
2312 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2313 continue;
2314
2315 if (visitedList.count(fragmentBlock.name) > 0)
2316 {
2317 for (gl::UniformBlock &block : mData.mUniformBlocks)
2318 {
2319 if (block.name == fragmentBlock.name)
2320 {
2321 block.fragmentStaticUse = fragmentBlock.staticUse;
2322 }
2323 }
2324
2325 continue;
2326 }
2327
2328 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2329 visitedList.insert(fragmentBlock.name);
2330 }
2331}
2332
2333void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2334{
2335 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2336 size_t firstBlockUniformIndex = mData.mUniforms.size();
2337 DefineUniformBlockMembers(interfaceBlock.fields, "", blockIndex, &mData.mUniforms);
2338 size_t lastBlockUniformIndex = mData.mUniforms.size();
2339
2340 std::vector<unsigned int> blockUniformIndexes;
2341 for (size_t blockUniformIndex = firstBlockUniformIndex;
2342 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2343 {
2344 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2345 }
2346
2347 if (interfaceBlock.arraySize > 0)
2348 {
2349 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2350 {
2351 UniformBlock block(interfaceBlock.name, true, arrayElement);
2352 block.memberUniformIndexes = blockUniformIndexes;
2353
2354 if (shaderType == GL_VERTEX_SHADER)
2355 {
2356 block.vertexStaticUse = interfaceBlock.staticUse;
2357 }
2358 else
2359 {
2360 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2361 block.fragmentStaticUse = interfaceBlock.staticUse;
2362 }
2363
2364 mData.mUniformBlocks.push_back(block);
2365 }
2366 }
2367 else
2368 {
2369 UniformBlock block(interfaceBlock.name, false, 0);
2370 block.memberUniformIndexes = blockUniformIndexes;
2371
2372 if (shaderType == GL_VERTEX_SHADER)
2373 {
2374 block.vertexStaticUse = interfaceBlock.staticUse;
2375 }
2376 else
2377 {
2378 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2379 block.fragmentStaticUse = interfaceBlock.staticUse;
2380 }
2381
2382 mData.mUniformBlocks.push_back(block);
2383 }
2384}
2385
2386template <typename T>
2387void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2388{
2389 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2390 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2391 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2392
2393 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2394 {
2395 // Do a cast conversion for boolean types. From the spec:
2396 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2397 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2398 for (GLsizei component = 0; component < count; ++component)
2399 {
2400 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2401 }
2402 }
2403 else
2404 {
Jamie Madill6cbf4382015-09-22 19:12:11 -04002405 // Invalide the validation cache if we modify the sampler data.
2406 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2407 {
2408 mCachedValidateSamplersResult.reset();
2409 }
2410
Jamie Madill62d31cb2015-09-11 13:25:51 -04002411 memcpy(destPointer, v, sizeof(T) * count);
2412 }
2413}
2414
2415template <size_t cols, size_t rows, typename T>
2416void Program::setMatrixUniformInternal(GLint location,
2417 GLsizei count,
2418 GLboolean transpose,
2419 const T *v)
2420{
2421 if (!transpose)
2422 {
2423 setUniformInternal(location, count * cols * rows, v);
2424 return;
2425 }
2426
2427 // Perform a transposing copy.
2428 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2429 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2430 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2431 for (GLsizei element = 0; element < count; ++element)
2432 {
2433 size_t elementOffset = element * rows * cols;
2434
2435 for (size_t row = 0; row < rows; ++row)
2436 {
2437 for (size_t col = 0; col < cols; ++col)
2438 {
2439 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2440 }
2441 }
2442 }
2443}
2444
2445template <typename DestT>
2446void Program::getUniformInternal(GLint location, DestT *dataOut) const
2447{
2448 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2449 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2450
2451 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2452
2453 GLenum componentType = VariableComponentType(uniform.type);
2454 if (componentType == GLTypeToGLenum<DestT>::value)
2455 {
2456 memcpy(dataOut, srcPointer, uniform.getElementSize());
2457 return;
2458 }
2459
2460 int components = VariableComponentCount(uniform.type) * uniform.elementCount();
2461
2462 switch (componentType)
2463 {
2464 case GL_INT:
2465 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2466 break;
2467 case GL_UNSIGNED_INT:
2468 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2469 break;
2470 case GL_BOOL:
2471 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2472 break;
2473 case GL_FLOAT:
2474 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2475 break;
2476 default:
2477 UNREACHABLE();
2478 }
2479}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480}