blob: bd16fd96e337b377bde08956b138b35a573736fa [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill80a6fc02015-08-21 16:53:16 -040014#include "common/BitSetIterator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
18#include "common/version.h"
19#include "compiler/translator/blocklayout.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050020#include "libANGLE/Data.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050022#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050023#include "libANGLE/renderer/Renderer.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050024#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill62d31cb2015-09-11 13:25:51 -040025#include "libANGLE/queryconversions.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050026
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027namespace gl
28{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000029
Geoff Lang7dd2e102014-11-10 15:19:26 -050030namespace
31{
32
Jamie Madill62d31cb2015-09-11 13:25:51 -040033void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
34{
35 stream->writeInt(var.type);
36 stream->writeInt(var.precision);
37 stream->writeString(var.name);
38 stream->writeString(var.mappedName);
39 stream->writeInt(var.arraySize);
40 stream->writeInt(var.staticUse);
41 stream->writeString(var.structName);
42 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050043}
44
Jamie Madill62d31cb2015-09-11 13:25:51 -040045void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
46{
47 var->type = stream->readInt<GLenum>();
48 var->precision = stream->readInt<GLenum>();
49 var->name = stream->readString();
50 var->mappedName = stream->readString();
51 var->arraySize = stream->readInt<unsigned int>();
52 var->staticUse = stream->readBool();
53 var->structName = stream->readString();
54}
55
Jamie Madill62d31cb2015-09-11 13:25:51 -040056// This simplified cast function doesn't need to worry about advanced concepts like
57// depth range values, or casting to bool.
58template <typename DestT, typename SrcT>
59DestT UniformStateQueryCast(SrcT value);
60
61// From-Float-To-Integer Casts
62template <>
63GLint UniformStateQueryCast(GLfloat value)
64{
65 return clampCast<GLint>(roundf(value));
66}
67
68template <>
69GLuint UniformStateQueryCast(GLfloat value)
70{
71 return clampCast<GLuint>(roundf(value));
72}
73
74// From-Integer-to-Integer Casts
75template <>
76GLint UniformStateQueryCast(GLuint value)
77{
78 return clampCast<GLint>(value);
79}
80
81template <>
82GLuint UniformStateQueryCast(GLint value)
83{
84 return clampCast<GLuint>(value);
85}
86
87// From-Boolean-to-Anything Casts
88template <>
89GLfloat UniformStateQueryCast(GLboolean value)
90{
91 return (value == GL_TRUE ? 1.0f : 0.0f);
92}
93
94template <>
95GLint UniformStateQueryCast(GLboolean value)
96{
97 return (value == GL_TRUE ? 1 : 0);
98}
99
100template <>
101GLuint UniformStateQueryCast(GLboolean value)
102{
103 return (value == GL_TRUE ? 1u : 0u);
104}
105
106// Default to static_cast
107template <typename DestT, typename SrcT>
108DestT UniformStateQueryCast(SrcT value)
109{
110 return static_cast<DestT>(value);
111}
112
113template <typename SrcT, typename DestT>
114void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
115{
116 for (int comp = 0; comp < components; ++comp)
117 {
118 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
119 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
120 size_t offset = comp * 4;
121 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
122 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
123 }
124}
125
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400126bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
127{
128 for (const LinkedUniform &uniform : list)
129 {
130 if (uniform.name == name)
131 return true;
132 }
133
134 return false;
135}
136
Jamie Madill62d31cb2015-09-11 13:25:51 -0400137} // anonymous namespace
138
Jamie Madill4a3c2342015-10-08 12:58:45 -0400139const char *const g_fakepath = "C:\\fakepath";
140
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000141AttributeBindings::AttributeBindings()
142{
143}
144
145AttributeBindings::~AttributeBindings()
146{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000147}
148
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400149InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000150{
151}
152
153InfoLog::~InfoLog()
154{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000155}
156
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400157size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000158{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400159 const std::string &logString = mStream.str();
160 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000161}
162
Geoff Lange1a27752015-10-05 13:16:04 -0400163void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000164{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400165 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000166
167 if (bufSize > 0)
168 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400169 const std::string str(mStream.str());
170
171 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000172 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400173 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
174 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000175 }
176
177 infoLog[index] = '\0';
178 }
179
180 if (length)
181 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400182 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000183 }
184}
185
186// append a santized message to the program info log.
187// The D3D compiler includes a fake file path in some of the warning or error
188// messages, so lets remove all occurrences of this fake file path from the log.
189void InfoLog::appendSanitized(const char *message)
190{
191 std::string msg(message);
192
193 size_t found;
194 do
195 {
196 found = msg.find(g_fakepath);
197 if (found != std::string::npos)
198 {
199 msg.erase(found, strlen(g_fakepath));
200 }
201 }
202 while (found != std::string::npos);
203
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400204 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000205}
206
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000207void InfoLog::reset()
208{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000209}
210
Geoff Lang7dd2e102014-11-10 15:19:26 -0500211VariableLocation::VariableLocation()
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400212 : name(),
213 element(0),
214 index(0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000215{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500216}
217
218VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index)
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400219 : name(name),
220 element(element),
221 index(index)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500222{
223}
224
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400225Program::Data::Data()
Geoff Lang70d0f492015-12-10 17:45:46 -0500226 : mLabel(),
227 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400228 mAttachedVertexShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500229 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
230 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400231{
232}
233
234Program::Data::~Data()
235{
236 if (mAttachedVertexShader != nullptr)
237 {
238 mAttachedVertexShader->release();
239 }
240
241 if (mAttachedFragmentShader != nullptr)
242 {
243 mAttachedFragmentShader->release();
244 }
245}
246
Geoff Lang70d0f492015-12-10 17:45:46 -0500247const std::string &Program::Data::getLabel()
248{
249 return mLabel;
250}
251
Jamie Madill62d31cb2015-09-11 13:25:51 -0400252const LinkedUniform *Program::Data::getUniformByName(const std::string &name) const
253{
254 for (const LinkedUniform &linkedUniform : mUniforms)
255 {
256 if (linkedUniform.name == name)
257 {
258 return &linkedUniform;
259 }
260 }
261
262 return nullptr;
263}
264
265GLint Program::Data::getUniformLocation(const std::string &name) const
266{
267 size_t subscript = GL_INVALID_INDEX;
268 std::string baseName = gl::ParseUniformName(name, &subscript);
269
270 for (size_t location = 0; location < mUniformLocations.size(); ++location)
271 {
272 const VariableLocation &uniformLocation = mUniformLocations[location];
273 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
274
275 if (uniform.name == baseName)
276 {
277 if ((uniform.isArray() && uniformLocation.element == subscript) ||
278 (subscript == GL_INVALID_INDEX))
279 {
280 return static_cast<GLint>(location);
281 }
282 }
283 }
284
285 return -1;
286}
287
288GLuint Program::Data::getUniformIndex(const std::string &name) const
289{
290 size_t subscript = GL_INVALID_INDEX;
291 std::string baseName = gl::ParseUniformName(name, &subscript);
292
293 // The app is not allowed to specify array indices other than 0 for arrays of basic types
294 if (subscript != 0 && subscript != GL_INVALID_INDEX)
295 {
296 return GL_INVALID_INDEX;
297 }
298
299 for (size_t index = 0; index < mUniforms.size(); index++)
300 {
301 const LinkedUniform &uniform = mUniforms[index];
302 if (uniform.name == baseName)
303 {
304 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
305 {
306 return static_cast<GLuint>(index);
307 }
308 }
309 }
310
311 return GL_INVALID_INDEX;
312}
313
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400314Program::Program(rx::ImplFactory *factory, ResourceManager *manager, GLuint handle)
315 : mProgram(factory->createProgram(mData)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400316 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500317 mLinked(false),
318 mDeleteStatus(false),
319 mRefCount(0),
320 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400321 mHandle(handle),
322 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500323{
324 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000325
326 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500327 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328}
329
330Program::~Program()
331{
332 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000333
Geoff Lang7dd2e102014-11-10 15:19:26 -0500334 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335}
336
Geoff Lang70d0f492015-12-10 17:45:46 -0500337void Program::setLabel(const std::string &label)
338{
339 mData.mLabel = label;
340}
341
342const std::string &Program::getLabel() const
343{
344 return mData.mLabel;
345}
346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347bool Program::attachShader(Shader *shader)
348{
349 if (shader->getType() == GL_VERTEX_SHADER)
350 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400351 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352 {
353 return false;
354 }
355
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400356 mData.mAttachedVertexShader = shader;
357 mData.mAttachedVertexShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358 }
359 else if (shader->getType() == GL_FRAGMENT_SHADER)
360 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400361 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362 {
363 return false;
364 }
365
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400366 mData.mAttachedFragmentShader = shader;
367 mData.mAttachedFragmentShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368 }
369 else UNREACHABLE();
370
371 return true;
372}
373
374bool Program::detachShader(Shader *shader)
375{
376 if (shader->getType() == GL_VERTEX_SHADER)
377 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400378 if (mData.mAttachedVertexShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000379 {
380 return false;
381 }
382
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400383 shader->release();
384 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 }
386 else if (shader->getType() == GL_FRAGMENT_SHADER)
387 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400388 if (mData.mAttachedFragmentShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389 {
390 return false;
391 }
392
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400393 shader->release();
394 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000395 }
396 else UNREACHABLE();
397
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398 return true;
399}
400
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000401int Program::getAttachedShadersCount() const
402{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400403 return (mData.mAttachedVertexShader ? 1 : 0) + (mData.mAttachedFragmentShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000404}
405
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000406void AttributeBindings::bindAttributeLocation(GLuint index, const char *name)
407{
408 if (index < MAX_VERTEX_ATTRIBS)
409 {
410 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
411 {
412 mAttributeBinding[i].erase(name);
413 }
414
415 mAttributeBinding[index].insert(name);
416 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000417}
418
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419void Program::bindAttributeLocation(GLuint index, const char *name)
420{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000421 mAttributeBindings.bindAttributeLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422}
423
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000424// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
425// compiling them into binaries, determining the attribute mappings, and collecting
426// a list of uniforms
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400427Error Program::link(const gl::Data &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000428{
429 unlink(false);
430
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000431 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000432 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000433
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400434 if (!mData.mAttachedFragmentShader || !mData.mAttachedFragmentShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500435 {
436 return Error(GL_NO_ERROR);
437 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400438 ASSERT(mData.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500439
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400440 if (!mData.mAttachedVertexShader || !mData.mAttachedVertexShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500441 {
442 return Error(GL_NO_ERROR);
443 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400444 ASSERT(mData.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500445
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400446 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mData.mAttachedVertexShader))
Jamie Madill437d2662014-12-05 14:23:35 -0500447 {
448 return Error(GL_NO_ERROR);
449 }
450
Jamie Madillada9ecc2015-08-17 12:53:37 -0400451 if (!linkVaryings(mInfoLog, mData.mAttachedVertexShader, mData.mAttachedFragmentShader))
452 {
453 return Error(GL_NO_ERROR);
454 }
455
Jamie Madillea918db2015-08-18 14:48:59 -0400456 if (!linkUniforms(mInfoLog, *data.caps))
457 {
458 return Error(GL_NO_ERROR);
459 }
460
Jamie Madille473dee2015-08-18 14:49:01 -0400461 if (!linkUniformBlocks(mInfoLog, *data.caps))
462 {
463 return Error(GL_NO_ERROR);
464 }
465
Jamie Madillccdf74b2015-08-18 10:46:12 -0400466 const auto &mergedVaryings = getMergedVaryings();
467
468 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, *data.caps))
469 {
470 return Error(GL_NO_ERROR);
471 }
472
Jamie Madill80a6fc02015-08-21 16:53:16 -0400473 linkOutputVariables();
474
Jamie Madillf5f4ad22015-09-02 18:32:38 +0000475 rx::LinkResult result = mProgram->link(data, mInfoLog);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500476 if (result.error.isError() || !result.linkSuccess)
477 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500478 return result.error;
479 }
480
Jamie Madillccdf74b2015-08-18 10:46:12 -0400481 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400482 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400483
Geoff Lang7dd2e102014-11-10 15:19:26 -0500484 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400485 return gl::Error(GL_NO_ERROR);
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000486}
487
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000488int AttributeBindings::getAttributeBinding(const std::string &name) const
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000489{
490 for (int location = 0; location < MAX_VERTEX_ATTRIBS; location++)
491 {
492 if (mAttributeBinding[location].find(name) != mAttributeBinding[location].end())
493 {
494 return location;
495 }
496 }
497
498 return -1;
499}
500
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000501// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502void Program::unlink(bool destroy)
503{
504 if (destroy) // Object being destructed
505 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400506 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400508 mData.mAttachedFragmentShader->release();
509 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510 }
511
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400512 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400514 mData.mAttachedVertexShader->release();
515 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517 }
518
Jamie Madillc349ec02015-08-21 16:53:12 -0400519 mData.mAttributes.clear();
Jamie Madill63805b42015-08-25 13:17:39 -0400520 mData.mActiveAttribLocationsMask.reset();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400521 mData.mTransformFeedbackVaryingVars.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400522 mData.mUniforms.clear();
523 mData.mUniformLocations.clear();
524 mData.mUniformBlocks.clear();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400525 mData.mOutputVariables.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500526
Geoff Lang7dd2e102014-11-10 15:19:26 -0500527 mValidated = false;
528
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000529 mLinked = false;
530}
531
Geoff Lange1a27752015-10-05 13:16:04 -0400532bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000533{
534 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000535}
536
Geoff Lang7dd2e102014-11-10 15:19:26 -0500537Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000538{
539 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000540
Geoff Lang7dd2e102014-11-10 15:19:26 -0500541#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
542 return Error(GL_NO_ERROR);
543#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400544 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
545 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000546 {
Jamie Madillf6113162015-05-07 11:49:21 -0400547 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500548 return Error(GL_NO_ERROR);
549 }
550
Geoff Langc46cc2f2015-10-01 17:16:20 -0400551 BinaryInputStream stream(binary, length);
552
Geoff Lang7dd2e102014-11-10 15:19:26 -0500553 int majorVersion = stream.readInt<int>();
554 int minorVersion = stream.readInt<int>();
555 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
556 {
Jamie Madillf6113162015-05-07 11:49:21 -0400557 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500558 return Error(GL_NO_ERROR);
559 }
560
561 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
562 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
563 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
564 {
Jamie Madillf6113162015-05-07 11:49:21 -0400565 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500566 return Error(GL_NO_ERROR);
567 }
568
Jamie Madill63805b42015-08-25 13:17:39 -0400569 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
570 "Too many vertex attribs for mask");
571 mData.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500572
Jamie Madill3da79b72015-04-27 11:09:17 -0400573 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400574 ASSERT(mData.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400575 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
576 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400577 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400578 LoadShaderVar(&stream, &attrib);
579 attrib.location = stream.readInt<int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400580 mData.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400581 }
582
Jamie Madill62d31cb2015-09-11 13:25:51 -0400583 unsigned int uniformCount = stream.readInt<unsigned int>();
584 ASSERT(mData.mUniforms.empty());
585 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
586 {
587 LinkedUniform uniform;
588 LoadShaderVar(&stream, &uniform);
589
590 uniform.blockIndex = stream.readInt<int>();
591 uniform.blockInfo.offset = stream.readInt<int>();
592 uniform.blockInfo.arrayStride = stream.readInt<int>();
593 uniform.blockInfo.matrixStride = stream.readInt<int>();
594 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
595
596 mData.mUniforms.push_back(uniform);
597 }
598
599 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
600 ASSERT(mData.mUniformLocations.empty());
601 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
602 uniformIndexIndex++)
603 {
604 VariableLocation variable;
605 stream.readString(&variable.name);
606 stream.readInt(&variable.element);
607 stream.readInt(&variable.index);
608
609 mData.mUniformLocations.push_back(variable);
610 }
611
612 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
613 ASSERT(mData.mUniformBlocks.empty());
614 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
615 ++uniformBlockIndex)
616 {
617 UniformBlock uniformBlock;
618 stream.readString(&uniformBlock.name);
619 stream.readBool(&uniformBlock.isArray);
620 stream.readInt(&uniformBlock.arrayElement);
621 stream.readInt(&uniformBlock.dataSize);
622 stream.readBool(&uniformBlock.vertexStaticUse);
623 stream.readBool(&uniformBlock.fragmentStaticUse);
624
625 unsigned int numMembers = stream.readInt<unsigned int>();
626 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
627 {
628 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
629 }
630
Jamie Madill62d31cb2015-09-11 13:25:51 -0400631 mData.mUniformBlocks.push_back(uniformBlock);
632 }
633
Brandon Jones1048ea72015-10-06 15:34:52 -0700634 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
635 ASSERT(mData.mTransformFeedbackVaryingVars.empty());
636 for (unsigned int transformFeedbackVaryingIndex = 0;
637 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
638 ++transformFeedbackVaryingIndex)
639 {
640 sh::Varying varying;
641 stream.readInt(&varying.arraySize);
642 stream.readInt(&varying.type);
643 stream.readString(&varying.name);
644
645 mData.mTransformFeedbackVaryingVars.push_back(varying);
646 }
647
Jamie Madillada9ecc2015-08-17 12:53:37 -0400648 stream.readInt(&mData.mTransformFeedbackBufferMode);
649
Jamie Madill80a6fc02015-08-21 16:53:16 -0400650 unsigned int outputVarCount = stream.readInt<unsigned int>();
651 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
652 {
653 int locationIndex = stream.readInt<int>();
654 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400655 stream.readInt(&locationData.element);
656 stream.readInt(&locationData.index);
657 stream.readString(&locationData.name);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400658 mData.mOutputVariables[locationIndex] = locationData;
659 }
660
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400661 stream.readInt(&mSamplerUniformRange.start);
662 stream.readInt(&mSamplerUniformRange.end);
663
Geoff Lang7dd2e102014-11-10 15:19:26 -0500664 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
665 if (result.error.isError() || !result.linkSuccess)
666 {
Geoff Langb543aff2014-09-30 14:52:54 -0400667 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000668 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000669
Geoff Lang7dd2e102014-11-10 15:19:26 -0500670 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400671 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500672#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
673}
674
675Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
676{
677 if (binaryFormat)
678 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400679 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500680 }
681
682 BinaryOutputStream stream;
683
Geoff Lang7dd2e102014-11-10 15:19:26 -0500684 stream.writeInt(ANGLE_MAJOR_VERSION);
685 stream.writeInt(ANGLE_MINOR_VERSION);
686 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
687
Jamie Madill63805b42015-08-25 13:17:39 -0400688 stream.writeInt(mData.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500689
Jamie Madillc349ec02015-08-21 16:53:12 -0400690 stream.writeInt(mData.mAttributes.size());
691 for (const sh::Attribute &attrib : mData.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400692 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400693 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400694 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400695 }
696
697 stream.writeInt(mData.mUniforms.size());
698 for (const gl::LinkedUniform &uniform : mData.mUniforms)
699 {
700 WriteShaderVar(&stream, uniform);
701
702 // FIXME: referenced
703
704 stream.writeInt(uniform.blockIndex);
705 stream.writeInt(uniform.blockInfo.offset);
706 stream.writeInt(uniform.blockInfo.arrayStride);
707 stream.writeInt(uniform.blockInfo.matrixStride);
708 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
709 }
710
711 stream.writeInt(mData.mUniformLocations.size());
712 for (const auto &variable : mData.mUniformLocations)
713 {
714 stream.writeString(variable.name);
715 stream.writeInt(variable.element);
716 stream.writeInt(variable.index);
717 }
718
719 stream.writeInt(mData.mUniformBlocks.size());
720 for (const UniformBlock &uniformBlock : mData.mUniformBlocks)
721 {
722 stream.writeString(uniformBlock.name);
723 stream.writeInt(uniformBlock.isArray);
724 stream.writeInt(uniformBlock.arrayElement);
725 stream.writeInt(uniformBlock.dataSize);
726
727 stream.writeInt(uniformBlock.vertexStaticUse);
728 stream.writeInt(uniformBlock.fragmentStaticUse);
729
730 stream.writeInt(uniformBlock.memberUniformIndexes.size());
731 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
732 {
733 stream.writeInt(memberUniformIndex);
734 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400735 }
736
Brandon Jones1048ea72015-10-06 15:34:52 -0700737 stream.writeInt(mData.mTransformFeedbackVaryingVars.size());
738 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
739 {
740 stream.writeInt(varying.arraySize);
741 stream.writeInt(varying.type);
742 stream.writeString(varying.name);
743 }
744
Jamie Madillada9ecc2015-08-17 12:53:37 -0400745 stream.writeInt(mData.mTransformFeedbackBufferMode);
746
Jamie Madill80a6fc02015-08-21 16:53:16 -0400747 stream.writeInt(mData.mOutputVariables.size());
748 for (const auto &outputPair : mData.mOutputVariables)
749 {
750 stream.writeInt(outputPair.first);
751 stream.writeInt(outputPair.second.element);
752 stream.writeInt(outputPair.second.index);
753 stream.writeString(outputPair.second.name);
754 }
755
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400756 stream.writeInt(mSamplerUniformRange.start);
757 stream.writeInt(mSamplerUniformRange.end);
758
Geoff Lang7dd2e102014-11-10 15:19:26 -0500759 gl::Error error = mProgram->save(&stream);
760 if (error.isError())
761 {
762 return error;
763 }
764
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700765 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500766 const void *streamData = stream.data();
767
768 if (streamLength > bufSize)
769 {
770 if (length)
771 {
772 *length = 0;
773 }
774
775 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
776 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
777 // sizes and then copy it.
778 return Error(GL_INVALID_OPERATION);
779 }
780
781 if (binary)
782 {
783 char *ptr = reinterpret_cast<char*>(binary);
784
785 memcpy(ptr, streamData, streamLength);
786 ptr += streamLength;
787
788 ASSERT(ptr - streamLength == binary);
789 }
790
791 if (length)
792 {
793 *length = streamLength;
794 }
795
796 return Error(GL_NO_ERROR);
797}
798
799GLint Program::getBinaryLength() const
800{
801 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400802 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500803 if (error.isError())
804 {
805 return 0;
806 }
807
808 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000809}
810
Geoff Langc5629752015-12-07 16:29:04 -0500811void Program::setBinaryRetrievableHint(bool retrievable)
812{
813 // TODO(jmadill) : replace with dirty bits
814 mProgram->setBinaryRetrievableHint(retrievable);
815 mData.mBinaryRetrieveableHint = retrievable;
816}
817
818bool Program::getBinaryRetrievableHint() const
819{
820 return mData.mBinaryRetrieveableHint;
821}
822
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000823void Program::release()
824{
825 mRefCount--;
826
827 if (mRefCount == 0 && mDeleteStatus)
828 {
829 mResourceManager->deleteProgram(mHandle);
830 }
831}
832
833void Program::addRef()
834{
835 mRefCount++;
836}
837
838unsigned int Program::getRefCount() const
839{
840 return mRefCount;
841}
842
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000843int Program::getInfoLogLength() const
844{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400845 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000846}
847
Geoff Lange1a27752015-10-05 13:16:04 -0400848void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000849{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000850 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000851}
852
Geoff Lange1a27752015-10-05 13:16:04 -0400853void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000854{
855 int total = 0;
856
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400857 if (mData.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000858 {
859 if (total < maxCount)
860 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400861 shaders[total] = mData.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200862 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000863 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000864 }
865
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400866 if (mData.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000867 {
868 if (total < maxCount)
869 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400870 shaders[total] = mData.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200871 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000872 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000873 }
874
875 if (count)
876 {
877 *count = total;
878 }
879}
880
Geoff Lange1a27752015-10-05 13:16:04 -0400881GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500882{
Jamie Madillc349ec02015-08-21 16:53:12 -0400883 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500884 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400885 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500886 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400887 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500888 }
889 }
890
Austin Kinrossb8af7232015-03-16 22:33:25 -0700891 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500892}
893
Jamie Madill63805b42015-08-25 13:17:39 -0400894bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400895{
Olli Etuaho401d9fe2015-08-26 10:19:59 +0300896 ASSERT(attribLocation < mData.mActiveAttribLocationsMask.size());
Jamie Madill63805b42015-08-25 13:17:39 -0400897 return mData.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500898}
899
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000900void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
901{
Jamie Madillc349ec02015-08-21 16:53:12 -0400902 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000903 {
904 if (bufsize > 0)
905 {
906 name[0] = '\0';
907 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500908
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000909 if (length)
910 {
911 *length = 0;
912 }
913
914 *type = GL_NONE;
915 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400916 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000917 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400918
919 size_t attributeIndex = 0;
920
921 for (const sh::Attribute &attribute : mData.mAttributes)
922 {
923 // Skip over inactive attributes
924 if (attribute.staticUse)
925 {
926 if (static_cast<size_t>(index) == attributeIndex)
927 {
928 break;
929 }
930 attributeIndex++;
931 }
932 }
933
934 ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
935 const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
936
937 if (bufsize > 0)
938 {
939 const char *string = attrib.name.c_str();
940
941 strncpy(name, string, bufsize);
942 name[bufsize - 1] = '\0';
943
944 if (length)
945 {
946 *length = static_cast<GLsizei>(strlen(name));
947 }
948 }
949
950 // Always a single 'type' instance
951 *size = 1;
952 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000953}
954
Geoff Lange1a27752015-10-05 13:16:04 -0400955GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000956{
Jamie Madillc349ec02015-08-21 16:53:12 -0400957 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400958 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400959 return 0;
960 }
961
962 GLint count = 0;
963
964 for (const sh::Attribute &attrib : mData.mAttributes)
965 {
966 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000967 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500968
969 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000970}
971
Geoff Lange1a27752015-10-05 13:16:04 -0400972GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000973{
Jamie Madillc349ec02015-08-21 16:53:12 -0400974 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400975 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400976 return 0;
977 }
978
979 size_t maxLength = 0;
980
981 for (const sh::Attribute &attrib : mData.mAttributes)
982 {
983 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500984 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400985 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500986 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000987 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500988
Jamie Madillc349ec02015-08-21 16:53:12 -0400989 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500990}
991
Geoff Lang7dd2e102014-11-10 15:19:26 -0500992GLint Program::getFragDataLocation(const std::string &name) const
993{
994 std::string baseName(name);
995 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400996 for (auto outputPair : mData.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000997 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400998 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500999 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1000 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001001 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001002 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001003 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001004 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001005}
1006
Geoff Lange1a27752015-10-05 13:16:04 -04001007void Program::getActiveUniform(GLuint index,
1008 GLsizei bufsize,
1009 GLsizei *length,
1010 GLint *size,
1011 GLenum *type,
1012 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001013{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001014 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001015 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001016 // index must be smaller than getActiveUniformCount()
1017 ASSERT(index < mData.mUniforms.size());
1018 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001019
1020 if (bufsize > 0)
1021 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001022 std::string string = uniform.name;
1023 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001024 {
1025 string += "[0]";
1026 }
1027
1028 strncpy(name, string.c_str(), bufsize);
1029 name[bufsize - 1] = '\0';
1030
1031 if (length)
1032 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001033 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001034 }
1035 }
1036
Jamie Madill62d31cb2015-09-11 13:25:51 -04001037 *size = uniform.elementCount();
1038 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001039 }
1040 else
1041 {
1042 if (bufsize > 0)
1043 {
1044 name[0] = '\0';
1045 }
1046
1047 if (length)
1048 {
1049 *length = 0;
1050 }
1051
1052 *size = 0;
1053 *type = GL_NONE;
1054 }
1055}
1056
Geoff Lange1a27752015-10-05 13:16:04 -04001057GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001058{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001059 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001060 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001061 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001062 }
1063 else
1064 {
1065 return 0;
1066 }
1067}
1068
Geoff Lange1a27752015-10-05 13:16:04 -04001069GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001070{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001071 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001072
1073 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001074 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001075 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001077 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001078 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001079 size_t length = uniform.name.length() + 1u;
1080 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081 {
1082 length += 3; // Counting in "[0]".
1083 }
1084 maxLength = std::max(length, maxLength);
1085 }
1086 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001087 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001088
Jamie Madill62d31cb2015-09-11 13:25:51 -04001089 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090}
1091
1092GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1093{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001094 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1095 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001096 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001097 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001098 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1099 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1100 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1101 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1102 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1103 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1104 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1105 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1106 default:
1107 UNREACHABLE();
1108 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001109 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001110 return 0;
1111}
1112
1113bool Program::isValidUniformLocation(GLint location) const
1114{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001115 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
1116 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001117}
1118
Jamie Madill62d31cb2015-09-11 13:25:51 -04001119const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001120{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001121 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1122 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001123}
1124
Jamie Madill62d31cb2015-09-11 13:25:51 -04001125GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001126{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001127 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001128}
1129
Jamie Madill62d31cb2015-09-11 13:25:51 -04001130GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001131{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001132 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001133}
1134
1135void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1136{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001137 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001138 mProgram->setUniform1fv(location, count, v);
1139}
1140
1141void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1142{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001143 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144 mProgram->setUniform2fv(location, count, v);
1145}
1146
1147void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1148{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001149 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001150 mProgram->setUniform3fv(location, count, v);
1151}
1152
1153void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1154{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001155 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001156 mProgram->setUniform4fv(location, count, v);
1157}
1158
1159void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1160{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001161 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001162 mProgram->setUniform1iv(location, count, v);
1163}
1164
1165void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1166{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001167 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001168 mProgram->setUniform2iv(location, count, v);
1169}
1170
1171void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1172{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001173 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001174 mProgram->setUniform3iv(location, count, v);
1175}
1176
1177void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1178{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001179 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001180 mProgram->setUniform4iv(location, count, v);
1181}
1182
1183void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1184{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001185 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001186 mProgram->setUniform1uiv(location, count, v);
1187}
1188
1189void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1190{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001191 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001192 mProgram->setUniform2uiv(location, count, v);
1193}
1194
1195void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1196{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001197 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001198 mProgram->setUniform3uiv(location, count, v);
1199}
1200
1201void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1202{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001203 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001204 mProgram->setUniform4uiv(location, count, v);
1205}
1206
1207void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1208{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001209 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001210 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1211}
1212
1213void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1214{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001215 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001216 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1217}
1218
1219void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1220{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001221 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001222 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1223}
1224
1225void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1226{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001227 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001228 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1229}
1230
1231void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1232{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001233 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001234 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1235}
1236
1237void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1238{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001239 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001240 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1241}
1242
1243void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1244{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001245 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001246 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1247}
1248
1249void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1250{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001251 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001252 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1253}
1254
1255void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1256{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001257 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001258 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1259}
1260
Geoff Lange1a27752015-10-05 13:16:04 -04001261void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001262{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001263 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001264}
1265
Geoff Lange1a27752015-10-05 13:16:04 -04001266void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001267{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001268 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001269}
1270
Geoff Lange1a27752015-10-05 13:16:04 -04001271void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001272{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001273 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001274}
1275
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001276void Program::flagForDeletion()
1277{
1278 mDeleteStatus = true;
1279}
1280
1281bool Program::isFlaggedForDeletion() const
1282{
1283 return mDeleteStatus;
1284}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001285
Brandon Jones43a53e22014-08-28 16:23:22 -07001286void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001287{
1288 mInfoLog.reset();
1289
Geoff Lang7dd2e102014-11-10 15:19:26 -05001290 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001291 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001292 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001293 }
1294 else
1295 {
Jamie Madillf6113162015-05-07 11:49:21 -04001296 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001297 }
1298}
1299
Geoff Lang7dd2e102014-11-10 15:19:26 -05001300bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1301{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001302 // Skip cache if we're using an infolog, so we get the full error.
1303 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1304 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1305 {
1306 return mCachedValidateSamplersResult.value();
1307 }
1308
1309 if (mTextureUnitTypesCache.empty())
1310 {
1311 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1312 }
1313 else
1314 {
1315 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1316 }
1317
1318 // if any two active samplers in a program are of different types, but refer to the same
1319 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1320 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1321 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1322 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1323 {
1324 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1325 ASSERT(uniform.isSampler());
1326
1327 if (!uniform.staticUse)
1328 continue;
1329
1330 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1331 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1332
1333 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1334 {
1335 GLuint textureUnit = dataPtr[arrayElement];
1336
1337 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1338 {
1339 if (infoLog)
1340 {
1341 (*infoLog) << "Sampler uniform (" << textureUnit
1342 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1343 << caps.maxCombinedTextureImageUnits << ")";
1344 }
1345
1346 mCachedValidateSamplersResult = false;
1347 return false;
1348 }
1349
1350 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1351 {
1352 if (textureType != mTextureUnitTypesCache[textureUnit])
1353 {
1354 if (infoLog)
1355 {
1356 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1357 "image unit ("
1358 << textureUnit << ").";
1359 }
1360
1361 mCachedValidateSamplersResult = false;
1362 return false;
1363 }
1364 }
1365 else
1366 {
1367 mTextureUnitTypesCache[textureUnit] = textureType;
1368 }
1369 }
1370 }
1371
1372 mCachedValidateSamplersResult = true;
1373 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001374}
1375
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001376bool Program::isValidated() const
1377{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378 return mValidated;
1379}
1380
Geoff Lange1a27752015-10-05 13:16:04 -04001381GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001383 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384}
1385
1386void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1387{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001388 ASSERT(uniformBlockIndex <
1389 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390
Jamie Madill62d31cb2015-09-11 13:25:51 -04001391 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001392
1393 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001394 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001395 std::string string = uniformBlock.name;
1396
Jamie Madill62d31cb2015-09-11 13:25:51 -04001397 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001398 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001399 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001400 }
1401
1402 strncpy(uniformBlockName, string.c_str(), bufSize);
1403 uniformBlockName[bufSize - 1] = '\0';
1404
1405 if (length)
1406 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001407 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001409 }
1410}
1411
Geoff Lang7dd2e102014-11-10 15:19:26 -05001412void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001413{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001414 ASSERT(uniformBlockIndex <
1415 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416
Jamie Madill62d31cb2015-09-11 13:25:51 -04001417 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001418
1419 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001420 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421 case GL_UNIFORM_BLOCK_DATA_SIZE:
1422 *params = static_cast<GLint>(uniformBlock.dataSize);
1423 break;
1424 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001425 *params =
1426 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427 break;
1428 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1429 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1430 break;
1431 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1432 {
1433 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1434 {
1435 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1436 }
1437 }
1438 break;
1439 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001440 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001441 break;
1442 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001443 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444 break;
1445 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001446 }
1447}
1448
Geoff Lange1a27752015-10-05 13:16:04 -04001449GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001450{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001451 int maxLength = 0;
1452
1453 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001454 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001455 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1457 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001458 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001459 if (!uniformBlock.name.empty())
1460 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001461 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462
1463 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001464 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001465
1466 maxLength = std::max(length + arrayLength, maxLength);
1467 }
1468 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001469 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001470
1471 return maxLength;
1472}
1473
Geoff Lange1a27752015-10-05 13:16:04 -04001474GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001476 size_t subscript = GL_INVALID_INDEX;
1477 std::string baseName = gl::ParseUniformName(name, &subscript);
1478
1479 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1480 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1481 {
1482 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1483 if (uniformBlock.name == baseName)
1484 {
1485 const bool arrayElementZero =
1486 (subscript == GL_INVALID_INDEX &&
1487 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1488 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1489 {
1490 return blockIndex;
1491 }
1492 }
1493 }
1494
1495 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001496}
1497
Jamie Madill62d31cb2015-09-11 13:25:51 -04001498const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001499{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001500 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1501 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001502}
1503
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001504void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1505{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001506 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001507 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001508}
1509
1510GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1511{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001512 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001513}
1514
1515void Program::resetUniformBlockBindings()
1516{
1517 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1518 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001519 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001520 }
Geoff Lang5d124a62015-09-15 13:03:27 -04001521 mData.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001522}
1523
Geoff Lang48dcae72014-02-05 16:28:24 -05001524void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1525{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001526 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001527 for (GLsizei i = 0; i < count; i++)
1528 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001529 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001530 }
1531
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001532 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001533}
1534
1535void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1536{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001537 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001538 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001539 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1540 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001541 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1542 if (length)
1543 {
1544 *length = lastNameIdx;
1545 }
1546 if (size)
1547 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001548 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001549 }
1550 if (type)
1551 {
1552 *type = varying.type;
1553 }
1554 if (name)
1555 {
1556 memcpy(name, varying.name.c_str(), lastNameIdx);
1557 name[lastNameIdx] = '\0';
1558 }
1559 }
1560}
1561
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001562GLsizei Program::getTransformFeedbackVaryingCount() const
1563{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001564 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001565 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001566 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001567 }
1568 else
1569 {
1570 return 0;
1571 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001572}
1573
1574GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1575{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001576 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001577 {
1578 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001579 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001580 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001581 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1582 }
1583
1584 return maxSize;
1585 }
1586 else
1587 {
1588 return 0;
1589 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001590}
1591
1592GLenum Program::getTransformFeedbackBufferMode() const
1593{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001594 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001595}
1596
Jamie Madillada9ecc2015-08-17 12:53:37 -04001597// static
1598bool Program::linkVaryings(InfoLog &infoLog,
1599 const Shader *vertexShader,
1600 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001601{
Jamie Madill4cff2472015-08-21 16:53:18 -04001602 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1603 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001604
Jamie Madill4cff2472015-08-21 16:53:18 -04001605 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001606 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001607 bool matched = false;
1608
1609 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001610 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001611 {
1612 continue;
1613 }
1614
Jamie Madill4cff2472015-08-21 16:53:18 -04001615 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001616 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001617 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001618 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001619 ASSERT(!input.isBuiltIn());
1620 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001621 {
1622 return false;
1623 }
1624
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625 matched = true;
1626 break;
1627 }
1628 }
1629
1630 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001631 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001633 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001634 return false;
1635 }
1636 }
1637
Jamie Madillada9ecc2015-08-17 12:53:37 -04001638 // TODO(jmadill): verify no unmatched vertex varyings?
1639
Geoff Lang7dd2e102014-11-10 15:19:26 -05001640 return true;
1641}
1642
Jamie Madill62d31cb2015-09-11 13:25:51 -04001643bool Program::linkUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Jamie Madillea918db2015-08-18 14:48:59 -04001644{
1645 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1646 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1647
1648 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001649 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001650
1651 for (const sh::Uniform &vertexUniform : vertexUniforms)
1652 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001653 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001654 }
1655
1656 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1657 {
1658 auto entry = linkedUniforms.find(fragmentUniform.name);
1659 if (entry != linkedUniforms.end())
1660 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001661 LinkedUniform *vertexUniform = &entry->second;
1662 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1663 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001664 {
1665 return false;
1666 }
1667 }
1668 }
1669
Jamie Madill62d31cb2015-09-11 13:25:51 -04001670 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1671 // Also check the maximum uniform vector and sampler counts.
1672 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1673 {
1674 return false;
1675 }
1676
1677 indexUniforms();
1678
Jamie Madillea918db2015-08-18 14:48:59 -04001679 return true;
1680}
1681
Jamie Madill62d31cb2015-09-11 13:25:51 -04001682void Program::indexUniforms()
1683{
1684 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1685 {
1686 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1687
1688 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1689 {
1690 if (!uniform.isBuiltIn())
1691 {
1692 // Assign in-order uniform locations
1693 mData.mUniformLocations.push_back(gl::VariableLocation(
1694 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)));
1695 }
1696 }
1697 }
1698}
1699
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1701{
Jamie Madillc4c744222015-11-04 09:39:47 -05001702 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1703 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704 {
1705 return false;
1706 }
1707
1708 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1709 {
Jamie Madillf6113162015-05-07 11:49:21 -04001710 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001711 return false;
1712 }
1713
1714 return true;
1715}
1716
1717// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001718bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001719 InfoLog &infoLog,
1720 const AttributeBindings &attributeBindings,
1721 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001722{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001723 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001724 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001725 GLuint maxAttribs = data.caps->maxVertexAttributes;
1726
1727 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001728 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001729 {
Jamie Madillf6113162015-05-07 11:49:21 -04001730 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001731 return false;
1732 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001733
Jamie Madillc349ec02015-08-21 16:53:12 -04001734 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001735
Jamie Madillc349ec02015-08-21 16:53:12 -04001736 // Link attributes that have a binding location
1737 for (sh::Attribute &attribute : mData.mAttributes)
1738 {
1739 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001740 ASSERT(attribute.staticUse);
1741
Jamie Madillc349ec02015-08-21 16:53:12 -04001742 int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
1743 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001744 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001745 attribute.location = bindingLocation;
1746 }
1747
1748 if (attribute.location != -1)
1749 {
1750 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001751 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001752
Jamie Madill63805b42015-08-25 13:17:39 -04001753 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001754 {
Jamie Madillf6113162015-05-07 11:49:21 -04001755 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001756 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001757
1758 return false;
1759 }
1760
Jamie Madill63805b42015-08-25 13:17:39 -04001761 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001762 {
Jamie Madill63805b42015-08-25 13:17:39 -04001763 const int regLocation = attribute.location + reg;
1764 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001765
1766 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001767 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001768 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001769 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001770 // TODO(jmadill): fix aliasing on ES2
1771 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001772 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001773 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001774 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001775 return false;
1776 }
1777 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001778 else
1779 {
Jamie Madill63805b42015-08-25 13:17:39 -04001780 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001781 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782
Jamie Madill63805b42015-08-25 13:17:39 -04001783 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001784 }
1785 }
1786 }
1787
1788 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001789 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001790 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791 ASSERT(attribute.staticUse);
1792
Jamie Madillc349ec02015-08-21 16:53:12 -04001793 // Not set by glBindAttribLocation or by location layout qualifier
1794 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795 {
Jamie Madill63805b42015-08-25 13:17:39 -04001796 int regs = VariableRegisterCount(attribute.type);
1797 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001798
Jamie Madill63805b42015-08-25 13:17:39 -04001799 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001800 {
Jamie Madillf6113162015-05-07 11:49:21 -04001801 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001802 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803 }
1804
Jamie Madillc349ec02015-08-21 16:53:12 -04001805 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001806 }
1807 }
1808
Jamie Madillc349ec02015-08-21 16:53:12 -04001809 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001810 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001811 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001812 ASSERT(attribute.location != -1);
1813 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001814
Jamie Madill63805b42015-08-25 13:17:39 -04001815 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001816 {
Jamie Madill63805b42015-08-25 13:17:39 -04001817 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001818 }
1819 }
1820
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821 return true;
1822}
1823
Jamie Madille473dee2015-08-18 14:49:01 -04001824bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001825{
Jamie Madille473dee2015-08-18 14:49:01 -04001826 const Shader &vertexShader = *mData.mAttachedVertexShader;
1827 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1828
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1830 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001831
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832 // Check that interface blocks defined in the vertex and fragment shaders are identical
1833 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1834 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001835
1836 GLuint vertexBlockCount = 0;
1837 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001838 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001839 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001840
1841 // Note: shared and std140 layouts are always considered active
1842 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1843 {
1844 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1845 {
1846 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1847 << caps.maxVertexUniformBlocks << ")";
1848 return false;
1849 }
1850 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851 }
Jamie Madille473dee2015-08-18 14:49:01 -04001852
1853 GLuint fragmentBlockCount = 0;
1854 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001855 {
Jamie Madille473dee2015-08-18 14:49:01 -04001856 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001857 if (entry != linkedUniformBlocks.end())
1858 {
1859 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1860 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1861 {
1862 return false;
1863 }
1864 }
Jamie Madille473dee2015-08-18 14:49:01 -04001865
Geoff Lang7dd2e102014-11-10 15:19:26 -05001866 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001867 if (fragmentInterfaceBlock.staticUse ||
1868 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001869 {
Jamie Madille473dee2015-08-18 14:49:01 -04001870 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871 {
Jamie Madille473dee2015-08-18 14:49:01 -04001872 infoLog
1873 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1874 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001875 return false;
1876 }
1877 }
1878 }
Jamie Madille473dee2015-08-18 14:49:01 -04001879
Geoff Lang7dd2e102014-11-10 15:19:26 -05001880 return true;
1881}
1882
1883bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1884 const sh::InterfaceBlock &fragmentInterfaceBlock)
1885{
1886 const char* blockName = vertexInterfaceBlock.name.c_str();
1887 // validate blocks for the same member types
1888 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1889 {
Jamie Madillf6113162015-05-07 11:49:21 -04001890 infoLog << "Types for interface block '" << blockName
1891 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892 return false;
1893 }
1894 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1895 {
Jamie Madillf6113162015-05-07 11:49:21 -04001896 infoLog << "Array sizes differ for interface block '" << blockName
1897 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001898 return false;
1899 }
1900 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
1901 {
Jamie Madillf6113162015-05-07 11:49:21 -04001902 infoLog << "Layout qualifiers differ for interface block '" << blockName
1903 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001904 return false;
1905 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001906 const unsigned int numBlockMembers =
1907 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001908 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1909 {
1910 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
1911 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
1912 if (vertexMember.name != fragmentMember.name)
1913 {
Jamie Madillf6113162015-05-07 11:49:21 -04001914 infoLog << "Name mismatch for field " << blockMemberIndex
1915 << " of interface block '" << blockName
1916 << "': (in vertex: '" << vertexMember.name
1917 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918 return false;
1919 }
1920 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
1921 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
1922 {
1923 return false;
1924 }
1925 }
1926 return true;
1927}
1928
1929bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
1930 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
1931{
1932 if (vertexVariable.type != fragmentVariable.type)
1933 {
Jamie Madillf6113162015-05-07 11:49:21 -04001934 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001935 return false;
1936 }
1937 if (vertexVariable.arraySize != fragmentVariable.arraySize)
1938 {
Jamie Madillf6113162015-05-07 11:49:21 -04001939 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001940 return false;
1941 }
1942 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
1943 {
Jamie Madillf6113162015-05-07 11:49:21 -04001944 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001945 return false;
1946 }
1947
1948 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
1949 {
Jamie Madillf6113162015-05-07 11:49:21 -04001950 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001951 return false;
1952 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001953 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001954 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1955 {
1956 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
1957 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
1958
1959 if (vertexMember.name != fragmentMember.name)
1960 {
Jamie Madillf6113162015-05-07 11:49:21 -04001961 infoLog << "Name mismatch for field '" << memberIndex
1962 << "' of " << variableName
1963 << ": (in vertex: '" << vertexMember.name
1964 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001965 return false;
1966 }
1967
1968 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
1969 vertexMember.name + "'";
1970
1971 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
1972 {
1973 return false;
1974 }
1975 }
1976
1977 return true;
1978}
1979
1980bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
1981{
Cooper Partin1acf4382015-06-12 12:38:57 -07001982#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
1983 const bool validatePrecision = true;
1984#else
1985 const bool validatePrecision = false;
1986#endif
1987
1988 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001989 {
1990 return false;
1991 }
1992
1993 return true;
1994}
1995
1996bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
1997{
1998 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
1999 {
2000 return false;
2001 }
2002
Jamie Madille9cc4692015-02-19 16:00:13 -05002003 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004 {
Jamie Madillf6113162015-05-07 11:49:21 -04002005 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006 return false;
2007 }
2008
2009 return true;
2010}
2011
Jamie Madillccdf74b2015-08-18 10:46:12 -04002012bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2013 const std::vector<const sh::Varying *> &varyings,
2014 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002015{
2016 size_t totalComponents = 0;
2017
Jamie Madillccdf74b2015-08-18 10:46:12 -04002018 std::set<std::string> uniqueNames;
2019
2020 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002021 {
2022 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002023 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002024 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002025 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002027 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002028 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002029 infoLog << "Two transform feedback varyings specify the same output variable ("
2030 << tfVaryingName << ").";
2031 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002032 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002033 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034
Geoff Lang1a683462015-09-29 15:09:59 -04002035 if (varying->isArray())
2036 {
2037 infoLog << "Capture of arrays is undefined and not supported.";
2038 return false;
2039 }
2040
Jamie Madillccdf74b2015-08-18 10:46:12 -04002041 // TODO(jmadill): Investigate implementation limits on D3D11
2042 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04002043 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002044 componentCount > caps.maxTransformFeedbackSeparateComponents)
2045 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002046 infoLog << "Transform feedback varying's " << varying->name << " components ("
2047 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002048 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002049 return false;
2050 }
2051
2052 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 found = true;
2054 break;
2055 }
2056 }
2057
Jamie Madill89bb70e2015-08-31 14:18:39 -04002058 if (tfVaryingName.find('[') != std::string::npos)
2059 {
Geoff Lang1a683462015-09-29 15:09:59 -04002060 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002061 return false;
2062 }
2063
Geoff Lang7dd2e102014-11-10 15:19:26 -05002064 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2065 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002066 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067 }
2068
Jamie Madillada9ecc2015-08-17 12:53:37 -04002069 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002070 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071 {
Jamie Madillf6113162015-05-07 11:49:21 -04002072 infoLog << "Transform feedback varying total components (" << totalComponents
2073 << ") exceed the maximum interleaved components ("
2074 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075 return false;
2076 }
2077
2078 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002079}
2080
Jamie Madillccdf74b2015-08-18 10:46:12 -04002081void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2082{
2083 // Gather the linked varyings that are used for transform feedback, they should all exist.
2084 mData.mTransformFeedbackVaryingVars.clear();
2085 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2086 {
2087 for (const sh::Varying *varying : varyings)
2088 {
2089 if (tfVaryingName == varying->name)
2090 {
2091 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2092 break;
2093 }
2094 }
2095 }
2096}
2097
2098std::vector<const sh::Varying *> Program::getMergedVaryings() const
2099{
2100 std::set<std::string> uniqueNames;
2101 std::vector<const sh::Varying *> varyings;
2102
2103 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2104 {
2105 if (uniqueNames.count(varying.name) == 0)
2106 {
2107 uniqueNames.insert(varying.name);
2108 varyings.push_back(&varying);
2109 }
2110 }
2111
2112 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2113 {
2114 if (uniqueNames.count(varying.name) == 0)
2115 {
2116 uniqueNames.insert(varying.name);
2117 varyings.push_back(&varying);
2118 }
2119 }
2120
2121 return varyings;
2122}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002123
2124void Program::linkOutputVariables()
2125{
2126 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2127 ASSERT(fragmentShader != nullptr);
2128
2129 // Skip this step for GLES2 shaders.
2130 if (fragmentShader->getShaderVersion() == 100)
2131 return;
2132
Jamie Madilla0a9e122015-09-02 15:54:30 -04002133 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002134
2135 // TODO(jmadill): any caps validation here?
2136
2137 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2138 outputVariableIndex++)
2139 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002140 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002141
2142 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2143 if (outputVariable.isBuiltIn())
2144 continue;
2145
2146 // Since multiple output locations must be specified, use 0 for non-specified locations.
2147 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2148
2149 ASSERT(outputVariable.staticUse);
2150
2151 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2152 elementIndex++)
2153 {
2154 const int location = baseLocation + elementIndex;
2155 ASSERT(mData.mOutputVariables.count(location) == 0);
2156 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2157 mData.mOutputVariables[location] =
2158 VariableLocation(outputVariable.name, element, outputVariableIndex);
2159 }
2160 }
2161}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002162
2163bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2164{
2165 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2166 VectorAndSamplerCount vsCounts;
2167
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002168 std::vector<LinkedUniform> samplerUniforms;
2169
Jamie Madill62d31cb2015-09-11 13:25:51 -04002170 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2171 {
2172 if (uniform.staticUse)
2173 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002174 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002175 }
2176 }
2177
2178 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2179 {
2180 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2181 << caps.maxVertexUniformVectors << ").";
2182 return false;
2183 }
2184
2185 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2186 {
2187 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2188 << caps.maxVertexTextureImageUnits << ").";
2189 return false;
2190 }
2191
2192 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2193 VectorAndSamplerCount fsCounts;
2194
2195 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2196 {
2197 if (uniform.staticUse)
2198 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002199 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002200 }
2201 }
2202
2203 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2204 {
2205 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2206 << caps.maxFragmentUniformVectors << ").";
2207 return false;
2208 }
2209
2210 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2211 {
2212 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2213 << caps.maxTextureImageUnits << ").";
2214 return false;
2215 }
2216
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002217 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2218 mSamplerUniformRange.end =
2219 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2220
2221 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2222
Jamie Madill62d31cb2015-09-11 13:25:51 -04002223 return true;
2224}
2225
2226Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002227 const std::string &fullName,
2228 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002229{
2230 VectorAndSamplerCount vectorAndSamplerCount;
2231
2232 if (uniform.isStruct())
2233 {
2234 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2235 {
2236 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2237
2238 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2239 {
2240 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2241 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2242
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002243 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002244 }
2245 }
2246
2247 return vectorAndSamplerCount;
2248 }
2249
2250 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002251 bool isSampler = IsSamplerType(uniform.type);
2252 if (!UniformInList(mData.getUniforms(), fullName) && !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002253 {
2254 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2255 uniform.arraySize, -1,
2256 sh::BlockMemberInfo::getDefaultBlockInfo());
2257 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002258
2259 // Store sampler uniforms separately, so we'll append them to the end of the list.
2260 if (isSampler)
2261 {
2262 samplerUniforms->push_back(linkedUniform);
2263 }
2264 else
2265 {
2266 mData.mUniforms.push_back(linkedUniform);
2267 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002268 }
2269
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002270 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002271
2272 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2273 // Likewise, don't count "real" uniforms towards sampler count.
2274 vectorAndSamplerCount.vectorCount =
2275 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002276 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002277
2278 return vectorAndSamplerCount;
2279}
2280
2281void Program::gatherInterfaceBlockInfo()
2282{
2283 std::set<std::string> visitedList;
2284
2285 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2286
2287 ASSERT(mData.mUniformBlocks.empty());
2288 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2289 {
2290 // Only 'packed' blocks are allowed to be considered inacive.
2291 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2292 continue;
2293
2294 if (visitedList.count(vertexBlock.name) > 0)
2295 continue;
2296
2297 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2298 visitedList.insert(vertexBlock.name);
2299 }
2300
2301 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2302
2303 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2304 {
2305 // Only 'packed' blocks are allowed to be considered inacive.
2306 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2307 continue;
2308
2309 if (visitedList.count(fragmentBlock.name) > 0)
2310 {
2311 for (gl::UniformBlock &block : mData.mUniformBlocks)
2312 {
2313 if (block.name == fragmentBlock.name)
2314 {
2315 block.fragmentStaticUse = fragmentBlock.staticUse;
2316 }
2317 }
2318
2319 continue;
2320 }
2321
2322 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2323 visitedList.insert(fragmentBlock.name);
2324 }
2325}
2326
Jamie Madill4a3c2342015-10-08 12:58:45 -04002327template <typename VarT>
2328void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2329 const std::string &prefix,
2330 int blockIndex)
2331{
2332 for (const VarT &field : fields)
2333 {
2334 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2335
2336 if (field.isStruct())
2337 {
2338 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2339 {
2340 const std::string uniformElementName =
2341 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2342 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2343 }
2344 }
2345 else
2346 {
2347 // If getBlockMemberInfo returns false, the uniform is optimized out.
2348 sh::BlockMemberInfo memberInfo;
2349 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2350 {
2351 continue;
2352 }
2353
2354 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2355 blockIndex, memberInfo);
2356
2357 // Since block uniforms have no location, we don't need to store them in the uniform
2358 // locations list.
2359 mData.mUniforms.push_back(newUniform);
2360 }
2361 }
2362}
2363
Jamie Madill62d31cb2015-09-11 13:25:51 -04002364void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2365{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002366 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2367 size_t blockSize = 0;
2368
2369 // Don't define this block at all if it's not active in the implementation.
2370 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2371 {
2372 return;
2373 }
2374
2375 // Track the first and last uniform index to determine the range of active uniforms in the
2376 // block.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002377 size_t firstBlockUniformIndex = mData.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002378 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002379 size_t lastBlockUniformIndex = mData.mUniforms.size();
2380
2381 std::vector<unsigned int> blockUniformIndexes;
2382 for (size_t blockUniformIndex = firstBlockUniformIndex;
2383 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2384 {
2385 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2386 }
2387
2388 if (interfaceBlock.arraySize > 0)
2389 {
2390 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2391 {
2392 UniformBlock block(interfaceBlock.name, true, arrayElement);
2393 block.memberUniformIndexes = blockUniformIndexes;
2394
2395 if (shaderType == GL_VERTEX_SHADER)
2396 {
2397 block.vertexStaticUse = interfaceBlock.staticUse;
2398 }
2399 else
2400 {
2401 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2402 block.fragmentStaticUse = interfaceBlock.staticUse;
2403 }
2404
Jamie Madill4a3c2342015-10-08 12:58:45 -04002405 // TODO(jmadill): Determine if we can ever have an inactive array element block.
2406 size_t blockElementSize = 0;
2407 if (!mProgram->getUniformBlockSize(block.nameWithArrayIndex(), &blockElementSize))
2408 {
2409 continue;
2410 }
2411
2412 ASSERT(blockElementSize == blockSize);
2413 block.dataSize = static_cast<unsigned int>(blockElementSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002414 mData.mUniformBlocks.push_back(block);
2415 }
2416 }
2417 else
2418 {
2419 UniformBlock block(interfaceBlock.name, false, 0);
2420 block.memberUniformIndexes = blockUniformIndexes;
2421
2422 if (shaderType == GL_VERTEX_SHADER)
2423 {
2424 block.vertexStaticUse = interfaceBlock.staticUse;
2425 }
2426 else
2427 {
2428 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2429 block.fragmentStaticUse = interfaceBlock.staticUse;
2430 }
2431
Jamie Madill4a3c2342015-10-08 12:58:45 -04002432 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002433 mData.mUniformBlocks.push_back(block);
2434 }
2435}
2436
2437template <typename T>
2438void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2439{
2440 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2441 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2442 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2443
2444 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2445 {
2446 // Do a cast conversion for boolean types. From the spec:
2447 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2448 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2449 for (GLsizei component = 0; component < count; ++component)
2450 {
2451 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2452 }
2453 }
2454 else
2455 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002456 // Invalide the validation cache if we modify the sampler data.
2457 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2458 {
2459 mCachedValidateSamplersResult.reset();
2460 }
2461
Jamie Madill62d31cb2015-09-11 13:25:51 -04002462 memcpy(destPointer, v, sizeof(T) * count);
2463 }
2464}
2465
2466template <size_t cols, size_t rows, typename T>
2467void Program::setMatrixUniformInternal(GLint location,
2468 GLsizei count,
2469 GLboolean transpose,
2470 const T *v)
2471{
2472 if (!transpose)
2473 {
2474 setUniformInternal(location, count * cols * rows, v);
2475 return;
2476 }
2477
2478 // Perform a transposing copy.
2479 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2480 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2481 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2482 for (GLsizei element = 0; element < count; ++element)
2483 {
2484 size_t elementOffset = element * rows * cols;
2485
2486 for (size_t row = 0; row < rows; ++row)
2487 {
2488 for (size_t col = 0; col < cols; ++col)
2489 {
2490 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2491 }
2492 }
2493 }
2494}
2495
2496template <typename DestT>
2497void Program::getUniformInternal(GLint location, DestT *dataOut) const
2498{
2499 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2500 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2501
2502 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2503
2504 GLenum componentType = VariableComponentType(uniform.type);
2505 if (componentType == GLTypeToGLenum<DestT>::value)
2506 {
2507 memcpy(dataOut, srcPointer, uniform.getElementSize());
2508 return;
2509 }
2510
2511 int components = VariableComponentCount(uniform.type) * uniform.elementCount();
2512
2513 switch (componentType)
2514 {
2515 case GL_INT:
2516 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2517 break;
2518 case GL_UNSIGNED_INT:
2519 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2520 break;
2521 case GL_BOOL:
2522 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2523 break;
2524 case GL_FLOAT:
2525 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2526 break;
2527 default:
2528 UNREACHABLE();
2529 }
2530}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531}