blob: 9347419db88449a2d9757371d662fdad87022ddc [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()
226 : mAttachedFragmentShader(nullptr),
227 mAttachedVertexShader(nullptr),
Geoff Lang1a683462015-09-29 15:09:59 -0400228 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400229{
230}
231
232Program::Data::~Data()
233{
234 if (mAttachedVertexShader != nullptr)
235 {
236 mAttachedVertexShader->release();
237 }
238
239 if (mAttachedFragmentShader != nullptr)
240 {
241 mAttachedFragmentShader->release();
242 }
243}
244
Jamie Madill62d31cb2015-09-11 13:25:51 -0400245const LinkedUniform *Program::Data::getUniformByName(const std::string &name) const
246{
247 for (const LinkedUniform &linkedUniform : mUniforms)
248 {
249 if (linkedUniform.name == name)
250 {
251 return &linkedUniform;
252 }
253 }
254
255 return nullptr;
256}
257
258GLint Program::Data::getUniformLocation(const std::string &name) const
259{
260 size_t subscript = GL_INVALID_INDEX;
261 std::string baseName = gl::ParseUniformName(name, &subscript);
262
263 for (size_t location = 0; location < mUniformLocations.size(); ++location)
264 {
265 const VariableLocation &uniformLocation = mUniformLocations[location];
266 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
267
268 if (uniform.name == baseName)
269 {
270 if ((uniform.isArray() && uniformLocation.element == subscript) ||
271 (subscript == GL_INVALID_INDEX))
272 {
273 return static_cast<GLint>(location);
274 }
275 }
276 }
277
278 return -1;
279}
280
281GLuint Program::Data::getUniformIndex(const std::string &name) const
282{
283 size_t subscript = GL_INVALID_INDEX;
284 std::string baseName = gl::ParseUniformName(name, &subscript);
285
286 // The app is not allowed to specify array indices other than 0 for arrays of basic types
287 if (subscript != 0 && subscript != GL_INVALID_INDEX)
288 {
289 return GL_INVALID_INDEX;
290 }
291
292 for (size_t index = 0; index < mUniforms.size(); index++)
293 {
294 const LinkedUniform &uniform = mUniforms[index];
295 if (uniform.name == baseName)
296 {
297 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
298 {
299 return static_cast<GLuint>(index);
300 }
301 }
302 }
303
304 return GL_INVALID_INDEX;
305}
306
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400307Program::Program(rx::ImplFactory *factory, ResourceManager *manager, GLuint handle)
308 : mProgram(factory->createProgram(mData)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400309 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500310 mLinked(false),
311 mDeleteStatus(false),
312 mRefCount(0),
313 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400314 mHandle(handle),
315 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500316{
317 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000318
319 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500320 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321}
322
323Program::~Program()
324{
325 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000326
Geoff Lang7dd2e102014-11-10 15:19:26 -0500327 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328}
329
330bool Program::attachShader(Shader *shader)
331{
332 if (shader->getType() == GL_VERTEX_SHADER)
333 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400334 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335 {
336 return false;
337 }
338
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400339 mData.mAttachedVertexShader = shader;
340 mData.mAttachedVertexShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341 }
342 else if (shader->getType() == GL_FRAGMENT_SHADER)
343 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400344 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345 {
346 return false;
347 }
348
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400349 mData.mAttachedFragmentShader = shader;
350 mData.mAttachedFragmentShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351 }
352 else UNREACHABLE();
353
354 return true;
355}
356
357bool Program::detachShader(Shader *shader)
358{
359 if (shader->getType() == GL_VERTEX_SHADER)
360 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400361 if (mData.mAttachedVertexShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362 {
363 return false;
364 }
365
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400366 shader->release();
367 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368 }
369 else if (shader->getType() == GL_FRAGMENT_SHADER)
370 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400371 if (mData.mAttachedFragmentShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372 {
373 return false;
374 }
375
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400376 shader->release();
377 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000378 }
379 else UNREACHABLE();
380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381 return true;
382}
383
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000384int Program::getAttachedShadersCount() const
385{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400386 return (mData.mAttachedVertexShader ? 1 : 0) + (mData.mAttachedFragmentShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000387}
388
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000389void AttributeBindings::bindAttributeLocation(GLuint index, const char *name)
390{
391 if (index < MAX_VERTEX_ATTRIBS)
392 {
393 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
394 {
395 mAttributeBinding[i].erase(name);
396 }
397
398 mAttributeBinding[index].insert(name);
399 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000400}
401
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402void Program::bindAttributeLocation(GLuint index, const char *name)
403{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000404 mAttributeBindings.bindAttributeLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405}
406
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
408// compiling them into binaries, determining the attribute mappings, and collecting
409// a list of uniforms
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400410Error Program::link(const gl::Data &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000411{
412 unlink(false);
413
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000414 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000415 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000416
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400417 if (!mData.mAttachedFragmentShader || !mData.mAttachedFragmentShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500418 {
419 return Error(GL_NO_ERROR);
420 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400421 ASSERT(mData.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500422
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400423 if (!mData.mAttachedVertexShader || !mData.mAttachedVertexShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500424 {
425 return Error(GL_NO_ERROR);
426 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400427 ASSERT(mData.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500428
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400429 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mData.mAttachedVertexShader))
Jamie Madill437d2662014-12-05 14:23:35 -0500430 {
431 return Error(GL_NO_ERROR);
432 }
433
Jamie Madillada9ecc2015-08-17 12:53:37 -0400434 if (!linkVaryings(mInfoLog, mData.mAttachedVertexShader, mData.mAttachedFragmentShader))
435 {
436 return Error(GL_NO_ERROR);
437 }
438
Jamie Madillea918db2015-08-18 14:48:59 -0400439 if (!linkUniforms(mInfoLog, *data.caps))
440 {
441 return Error(GL_NO_ERROR);
442 }
443
Jamie Madille473dee2015-08-18 14:49:01 -0400444 if (!linkUniformBlocks(mInfoLog, *data.caps))
445 {
446 return Error(GL_NO_ERROR);
447 }
448
Jamie Madillccdf74b2015-08-18 10:46:12 -0400449 const auto &mergedVaryings = getMergedVaryings();
450
451 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, *data.caps))
452 {
453 return Error(GL_NO_ERROR);
454 }
455
Jamie Madill80a6fc02015-08-21 16:53:16 -0400456 linkOutputVariables();
457
Jamie Madillf5f4ad22015-09-02 18:32:38 +0000458 rx::LinkResult result = mProgram->link(data, mInfoLog);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500459 if (result.error.isError() || !result.linkSuccess)
460 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500461 return result.error;
462 }
463
Jamie Madillccdf74b2015-08-18 10:46:12 -0400464 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400465 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400466
Geoff Lang7dd2e102014-11-10 15:19:26 -0500467 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400468 return gl::Error(GL_NO_ERROR);
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000469}
470
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000471int AttributeBindings::getAttributeBinding(const std::string &name) const
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000472{
473 for (int location = 0; location < MAX_VERTEX_ATTRIBS; location++)
474 {
475 if (mAttributeBinding[location].find(name) != mAttributeBinding[location].end())
476 {
477 return location;
478 }
479 }
480
481 return -1;
482}
483
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000484// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485void Program::unlink(bool destroy)
486{
487 if (destroy) // Object being destructed
488 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400489 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400491 mData.mAttachedFragmentShader->release();
492 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493 }
494
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400495 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400497 mData.mAttachedVertexShader->release();
498 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500 }
501
Jamie Madillc349ec02015-08-21 16:53:12 -0400502 mData.mAttributes.clear();
Jamie Madill63805b42015-08-25 13:17:39 -0400503 mData.mActiveAttribLocationsMask.reset();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400504 mData.mTransformFeedbackVaryingVars.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400505 mData.mUniforms.clear();
506 mData.mUniformLocations.clear();
507 mData.mUniformBlocks.clear();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400508 mData.mOutputVariables.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500509
Geoff Lang7dd2e102014-11-10 15:19:26 -0500510 mValidated = false;
511
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000512 mLinked = false;
513}
514
Geoff Lange1a27752015-10-05 13:16:04 -0400515bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000516{
517 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518}
519
Geoff Lang7dd2e102014-11-10 15:19:26 -0500520Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000521{
522 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000523
Geoff Lang7dd2e102014-11-10 15:19:26 -0500524#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
525 return Error(GL_NO_ERROR);
526#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400527 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
528 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000529 {
Jamie Madillf6113162015-05-07 11:49:21 -0400530 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500531 return Error(GL_NO_ERROR);
532 }
533
Geoff Langc46cc2f2015-10-01 17:16:20 -0400534 BinaryInputStream stream(binary, length);
535
Geoff Lang7dd2e102014-11-10 15:19:26 -0500536 int majorVersion = stream.readInt<int>();
537 int minorVersion = stream.readInt<int>();
538 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
539 {
Jamie Madillf6113162015-05-07 11:49:21 -0400540 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500541 return Error(GL_NO_ERROR);
542 }
543
544 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
545 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
546 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
547 {
Jamie Madillf6113162015-05-07 11:49:21 -0400548 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500549 return Error(GL_NO_ERROR);
550 }
551
Jamie Madill63805b42015-08-25 13:17:39 -0400552 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
553 "Too many vertex attribs for mask");
554 mData.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500555
Jamie Madill3da79b72015-04-27 11:09:17 -0400556 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400557 ASSERT(mData.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400558 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
559 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400560 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400561 LoadShaderVar(&stream, &attrib);
562 attrib.location = stream.readInt<int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400563 mData.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400564 }
565
Jamie Madill62d31cb2015-09-11 13:25:51 -0400566 unsigned int uniformCount = stream.readInt<unsigned int>();
567 ASSERT(mData.mUniforms.empty());
568 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
569 {
570 LinkedUniform uniform;
571 LoadShaderVar(&stream, &uniform);
572
573 uniform.blockIndex = stream.readInt<int>();
574 uniform.blockInfo.offset = stream.readInt<int>();
575 uniform.blockInfo.arrayStride = stream.readInt<int>();
576 uniform.blockInfo.matrixStride = stream.readInt<int>();
577 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
578
579 mData.mUniforms.push_back(uniform);
580 }
581
582 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
583 ASSERT(mData.mUniformLocations.empty());
584 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
585 uniformIndexIndex++)
586 {
587 VariableLocation variable;
588 stream.readString(&variable.name);
589 stream.readInt(&variable.element);
590 stream.readInt(&variable.index);
591
592 mData.mUniformLocations.push_back(variable);
593 }
594
595 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
596 ASSERT(mData.mUniformBlocks.empty());
597 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
598 ++uniformBlockIndex)
599 {
600 UniformBlock uniformBlock;
601 stream.readString(&uniformBlock.name);
602 stream.readBool(&uniformBlock.isArray);
603 stream.readInt(&uniformBlock.arrayElement);
604 stream.readInt(&uniformBlock.dataSize);
605 stream.readBool(&uniformBlock.vertexStaticUse);
606 stream.readBool(&uniformBlock.fragmentStaticUse);
607
608 unsigned int numMembers = stream.readInt<unsigned int>();
609 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
610 {
611 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
612 }
613
Jamie Madill62d31cb2015-09-11 13:25:51 -0400614 mData.mUniformBlocks.push_back(uniformBlock);
615 }
616
Brandon Jones1048ea72015-10-06 15:34:52 -0700617 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
618 ASSERT(mData.mTransformFeedbackVaryingVars.empty());
619 for (unsigned int transformFeedbackVaryingIndex = 0;
620 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
621 ++transformFeedbackVaryingIndex)
622 {
623 sh::Varying varying;
624 stream.readInt(&varying.arraySize);
625 stream.readInt(&varying.type);
626 stream.readString(&varying.name);
627
628 mData.mTransformFeedbackVaryingVars.push_back(varying);
629 }
630
Jamie Madillada9ecc2015-08-17 12:53:37 -0400631 stream.readInt(&mData.mTransformFeedbackBufferMode);
632
Jamie Madill80a6fc02015-08-21 16:53:16 -0400633 unsigned int outputVarCount = stream.readInt<unsigned int>();
634 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
635 {
636 int locationIndex = stream.readInt<int>();
637 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400638 stream.readInt(&locationData.element);
639 stream.readInt(&locationData.index);
640 stream.readString(&locationData.name);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400641 mData.mOutputVariables[locationIndex] = locationData;
642 }
643
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400644 stream.readInt(&mSamplerUniformRange.start);
645 stream.readInt(&mSamplerUniformRange.end);
646
Geoff Lang7dd2e102014-11-10 15:19:26 -0500647 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
648 if (result.error.isError() || !result.linkSuccess)
649 {
Geoff Langb543aff2014-09-30 14:52:54 -0400650 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000651 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000652
Geoff Lang7dd2e102014-11-10 15:19:26 -0500653 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400654 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500655#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
656}
657
658Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
659{
660 if (binaryFormat)
661 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400662 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500663 }
664
665 BinaryOutputStream stream;
666
Geoff Lang7dd2e102014-11-10 15:19:26 -0500667 stream.writeInt(ANGLE_MAJOR_VERSION);
668 stream.writeInt(ANGLE_MINOR_VERSION);
669 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
670
Jamie Madill63805b42015-08-25 13:17:39 -0400671 stream.writeInt(mData.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500672
Jamie Madillc349ec02015-08-21 16:53:12 -0400673 stream.writeInt(mData.mAttributes.size());
674 for (const sh::Attribute &attrib : mData.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400675 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400676 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400677 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400678 }
679
680 stream.writeInt(mData.mUniforms.size());
681 for (const gl::LinkedUniform &uniform : mData.mUniforms)
682 {
683 WriteShaderVar(&stream, uniform);
684
685 // FIXME: referenced
686
687 stream.writeInt(uniform.blockIndex);
688 stream.writeInt(uniform.blockInfo.offset);
689 stream.writeInt(uniform.blockInfo.arrayStride);
690 stream.writeInt(uniform.blockInfo.matrixStride);
691 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
692 }
693
694 stream.writeInt(mData.mUniformLocations.size());
695 for (const auto &variable : mData.mUniformLocations)
696 {
697 stream.writeString(variable.name);
698 stream.writeInt(variable.element);
699 stream.writeInt(variable.index);
700 }
701
702 stream.writeInt(mData.mUniformBlocks.size());
703 for (const UniformBlock &uniformBlock : mData.mUniformBlocks)
704 {
705 stream.writeString(uniformBlock.name);
706 stream.writeInt(uniformBlock.isArray);
707 stream.writeInt(uniformBlock.arrayElement);
708 stream.writeInt(uniformBlock.dataSize);
709
710 stream.writeInt(uniformBlock.vertexStaticUse);
711 stream.writeInt(uniformBlock.fragmentStaticUse);
712
713 stream.writeInt(uniformBlock.memberUniformIndexes.size());
714 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
715 {
716 stream.writeInt(memberUniformIndex);
717 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400718 }
719
Brandon Jones1048ea72015-10-06 15:34:52 -0700720 stream.writeInt(mData.mTransformFeedbackVaryingVars.size());
721 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
722 {
723 stream.writeInt(varying.arraySize);
724 stream.writeInt(varying.type);
725 stream.writeString(varying.name);
726 }
727
Jamie Madillada9ecc2015-08-17 12:53:37 -0400728 stream.writeInt(mData.mTransformFeedbackBufferMode);
729
Jamie Madill80a6fc02015-08-21 16:53:16 -0400730 stream.writeInt(mData.mOutputVariables.size());
731 for (const auto &outputPair : mData.mOutputVariables)
732 {
733 stream.writeInt(outputPair.first);
734 stream.writeInt(outputPair.second.element);
735 stream.writeInt(outputPair.second.index);
736 stream.writeString(outputPair.second.name);
737 }
738
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400739 stream.writeInt(mSamplerUniformRange.start);
740 stream.writeInt(mSamplerUniformRange.end);
741
Geoff Lang7dd2e102014-11-10 15:19:26 -0500742 gl::Error error = mProgram->save(&stream);
743 if (error.isError())
744 {
745 return error;
746 }
747
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700748 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500749 const void *streamData = stream.data();
750
751 if (streamLength > bufSize)
752 {
753 if (length)
754 {
755 *length = 0;
756 }
757
758 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
759 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
760 // sizes and then copy it.
761 return Error(GL_INVALID_OPERATION);
762 }
763
764 if (binary)
765 {
766 char *ptr = reinterpret_cast<char*>(binary);
767
768 memcpy(ptr, streamData, streamLength);
769 ptr += streamLength;
770
771 ASSERT(ptr - streamLength == binary);
772 }
773
774 if (length)
775 {
776 *length = streamLength;
777 }
778
779 return Error(GL_NO_ERROR);
780}
781
782GLint Program::getBinaryLength() const
783{
784 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400785 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500786 if (error.isError())
787 {
788 return 0;
789 }
790
791 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000792}
793
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000794void Program::release()
795{
796 mRefCount--;
797
798 if (mRefCount == 0 && mDeleteStatus)
799 {
800 mResourceManager->deleteProgram(mHandle);
801 }
802}
803
804void Program::addRef()
805{
806 mRefCount++;
807}
808
809unsigned int Program::getRefCount() const
810{
811 return mRefCount;
812}
813
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000814int Program::getInfoLogLength() const
815{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400816 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000817}
818
Geoff Lange1a27752015-10-05 13:16:04 -0400819void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000820{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000821 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000822}
823
Geoff Lange1a27752015-10-05 13:16:04 -0400824void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000825{
826 int total = 0;
827
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400828 if (mData.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000829 {
830 if (total < maxCount)
831 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400832 shaders[total] = mData.mAttachedVertexShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000833 }
834
835 total++;
836 }
837
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400838 if (mData.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000839 {
840 if (total < maxCount)
841 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400842 shaders[total] = mData.mAttachedFragmentShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000843 }
844
845 total++;
846 }
847
848 if (count)
849 {
850 *count = total;
851 }
852}
853
Geoff Lange1a27752015-10-05 13:16:04 -0400854GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500855{
Jamie Madillc349ec02015-08-21 16:53:12 -0400856 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500857 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400858 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500859 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400860 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500861 }
862 }
863
Austin Kinrossb8af7232015-03-16 22:33:25 -0700864 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500865}
866
Jamie Madill63805b42015-08-25 13:17:39 -0400867bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400868{
Olli Etuaho401d9fe2015-08-26 10:19:59 +0300869 ASSERT(attribLocation < mData.mActiveAttribLocationsMask.size());
Jamie Madill63805b42015-08-25 13:17:39 -0400870 return mData.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500871}
872
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000873void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
874{
Jamie Madillc349ec02015-08-21 16:53:12 -0400875 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000876 {
877 if (bufsize > 0)
878 {
879 name[0] = '\0';
880 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500881
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000882 if (length)
883 {
884 *length = 0;
885 }
886
887 *type = GL_NONE;
888 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400889 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000890 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400891
892 size_t attributeIndex = 0;
893
894 for (const sh::Attribute &attribute : mData.mAttributes)
895 {
896 // Skip over inactive attributes
897 if (attribute.staticUse)
898 {
899 if (static_cast<size_t>(index) == attributeIndex)
900 {
901 break;
902 }
903 attributeIndex++;
904 }
905 }
906
907 ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
908 const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
909
910 if (bufsize > 0)
911 {
912 const char *string = attrib.name.c_str();
913
914 strncpy(name, string, bufsize);
915 name[bufsize - 1] = '\0';
916
917 if (length)
918 {
919 *length = static_cast<GLsizei>(strlen(name));
920 }
921 }
922
923 // Always a single 'type' instance
924 *size = 1;
925 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000926}
927
Geoff Lange1a27752015-10-05 13:16:04 -0400928GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000929{
Jamie Madillc349ec02015-08-21 16:53:12 -0400930 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400931 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400932 return 0;
933 }
934
935 GLint count = 0;
936
937 for (const sh::Attribute &attrib : mData.mAttributes)
938 {
939 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000940 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500941
942 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000943}
944
Geoff Lange1a27752015-10-05 13:16:04 -0400945GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000946{
Jamie Madillc349ec02015-08-21 16:53:12 -0400947 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400948 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400949 return 0;
950 }
951
952 size_t maxLength = 0;
953
954 for (const sh::Attribute &attrib : mData.mAttributes)
955 {
956 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500957 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400958 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500959 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000960 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500961
Jamie Madillc349ec02015-08-21 16:53:12 -0400962 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500963}
964
Geoff Lang7dd2e102014-11-10 15:19:26 -0500965GLint Program::getFragDataLocation(const std::string &name) const
966{
967 std::string baseName(name);
968 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400969 for (auto outputPair : mData.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000970 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400971 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500972 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
973 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400974 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500975 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000976 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500977 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000978}
979
Geoff Lange1a27752015-10-05 13:16:04 -0400980void Program::getActiveUniform(GLuint index,
981 GLsizei bufsize,
982 GLsizei *length,
983 GLint *size,
984 GLenum *type,
985 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000986{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500987 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000988 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400989 // index must be smaller than getActiveUniformCount()
990 ASSERT(index < mData.mUniforms.size());
991 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500992
993 if (bufsize > 0)
994 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400995 std::string string = uniform.name;
996 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500997 {
998 string += "[0]";
999 }
1000
1001 strncpy(name, string.c_str(), bufsize);
1002 name[bufsize - 1] = '\0';
1003
1004 if (length)
1005 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001006 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001007 }
1008 }
1009
Jamie Madill62d31cb2015-09-11 13:25:51 -04001010 *size = uniform.elementCount();
1011 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001012 }
1013 else
1014 {
1015 if (bufsize > 0)
1016 {
1017 name[0] = '\0';
1018 }
1019
1020 if (length)
1021 {
1022 *length = 0;
1023 }
1024
1025 *size = 0;
1026 *type = GL_NONE;
1027 }
1028}
1029
Geoff Lange1a27752015-10-05 13:16:04 -04001030GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001031{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001032 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001033 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001034 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001035 }
1036 else
1037 {
1038 return 0;
1039 }
1040}
1041
Geoff Lange1a27752015-10-05 13:16:04 -04001042GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001043{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001044 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001045
1046 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001047 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001048 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001049 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001050 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001051 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001052 size_t length = uniform.name.length() + 1u;
1053 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001054 {
1055 length += 3; // Counting in "[0]".
1056 }
1057 maxLength = std::max(length, maxLength);
1058 }
1059 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001060 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001061
Jamie Madill62d31cb2015-09-11 13:25:51 -04001062 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001063}
1064
1065GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1066{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001067 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1068 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001069 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001070 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001071 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1072 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1073 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1074 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1075 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1076 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1077 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1078 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1079 default:
1080 UNREACHABLE();
1081 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001082 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001083 return 0;
1084}
1085
1086bool Program::isValidUniformLocation(GLint location) const
1087{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001088 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
1089 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090}
1091
Jamie Madill62d31cb2015-09-11 13:25:51 -04001092const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001093{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001094 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1095 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001096}
1097
Jamie Madill62d31cb2015-09-11 13:25:51 -04001098GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001100 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001101}
1102
Jamie Madill62d31cb2015-09-11 13:25:51 -04001103GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001104{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001105 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106}
1107
1108void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1109{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001110 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001111 mProgram->setUniform1fv(location, count, v);
1112}
1113
1114void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1115{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001116 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001117 mProgram->setUniform2fv(location, count, v);
1118}
1119
1120void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1121{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001122 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001123 mProgram->setUniform3fv(location, count, v);
1124}
1125
1126void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1127{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001128 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001129 mProgram->setUniform4fv(location, count, v);
1130}
1131
1132void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1133{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001134 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001135 mProgram->setUniform1iv(location, count, v);
1136}
1137
1138void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1139{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001140 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001141 mProgram->setUniform2iv(location, count, v);
1142}
1143
1144void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1145{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001146 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147 mProgram->setUniform3iv(location, count, v);
1148}
1149
1150void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1151{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001152 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001153 mProgram->setUniform4iv(location, count, v);
1154}
1155
1156void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1157{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001158 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001159 mProgram->setUniform1uiv(location, count, v);
1160}
1161
1162void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1163{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001164 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001165 mProgram->setUniform2uiv(location, count, v);
1166}
1167
1168void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1169{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001170 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001171 mProgram->setUniform3uiv(location, count, v);
1172}
1173
1174void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1175{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001176 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001177 mProgram->setUniform4uiv(location, count, v);
1178}
1179
1180void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1181{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001182 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001183 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1184}
1185
1186void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1187{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001188 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001189 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1190}
1191
1192void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1193{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001194 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001195 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1196}
1197
1198void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1199{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001200 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001201 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1202}
1203
1204void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1205{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001206 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001207 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1208}
1209
1210void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1211{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001212 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001213 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1214}
1215
1216void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1217{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001218 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001219 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1220}
1221
1222void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1223{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001224 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001225 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1226}
1227
1228void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1229{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001230 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001231 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1232}
1233
Geoff Lange1a27752015-10-05 13:16:04 -04001234void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001235{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001236 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001237}
1238
Geoff Lange1a27752015-10-05 13:16:04 -04001239void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001240{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001241 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001242}
1243
Geoff Lange1a27752015-10-05 13:16:04 -04001244void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001245{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001246 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001247}
1248
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001249void Program::flagForDeletion()
1250{
1251 mDeleteStatus = true;
1252}
1253
1254bool Program::isFlaggedForDeletion() const
1255{
1256 return mDeleteStatus;
1257}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001258
Brandon Jones43a53e22014-08-28 16:23:22 -07001259void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001260{
1261 mInfoLog.reset();
1262
Geoff Lang7dd2e102014-11-10 15:19:26 -05001263 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001264 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001265 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001266 }
1267 else
1268 {
Jamie Madillf6113162015-05-07 11:49:21 -04001269 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001270 }
1271}
1272
Geoff Lang7dd2e102014-11-10 15:19:26 -05001273bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1274{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001275 // Skip cache if we're using an infolog, so we get the full error.
1276 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1277 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1278 {
1279 return mCachedValidateSamplersResult.value();
1280 }
1281
1282 if (mTextureUnitTypesCache.empty())
1283 {
1284 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1285 }
1286 else
1287 {
1288 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1289 }
1290
1291 // if any two active samplers in a program are of different types, but refer to the same
1292 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1293 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1294 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1295 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1296 {
1297 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1298 ASSERT(uniform.isSampler());
1299
1300 if (!uniform.staticUse)
1301 continue;
1302
1303 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1304 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1305
1306 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1307 {
1308 GLuint textureUnit = dataPtr[arrayElement];
1309
1310 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1311 {
1312 if (infoLog)
1313 {
1314 (*infoLog) << "Sampler uniform (" << textureUnit
1315 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1316 << caps.maxCombinedTextureImageUnits << ")";
1317 }
1318
1319 mCachedValidateSamplersResult = false;
1320 return false;
1321 }
1322
1323 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1324 {
1325 if (textureType != mTextureUnitTypesCache[textureUnit])
1326 {
1327 if (infoLog)
1328 {
1329 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1330 "image unit ("
1331 << textureUnit << ").";
1332 }
1333
1334 mCachedValidateSamplersResult = false;
1335 return false;
1336 }
1337 }
1338 else
1339 {
1340 mTextureUnitTypesCache[textureUnit] = textureType;
1341 }
1342 }
1343 }
1344
1345 mCachedValidateSamplersResult = true;
1346 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347}
1348
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001349bool Program::isValidated() const
1350{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001351 return mValidated;
1352}
1353
Geoff Lange1a27752015-10-05 13:16:04 -04001354GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001355{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001356 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357}
1358
1359void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1360{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001361 ASSERT(uniformBlockIndex <
1362 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001363
Jamie Madill62d31cb2015-09-11 13:25:51 -04001364 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365
1366 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001367 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368 std::string string = uniformBlock.name;
1369
Jamie Madill62d31cb2015-09-11 13:25:51 -04001370 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001372 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373 }
1374
1375 strncpy(uniformBlockName, string.c_str(), bufSize);
1376 uniformBlockName[bufSize - 1] = '\0';
1377
1378 if (length)
1379 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001380 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001381 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001382 }
1383}
1384
Geoff Lang7dd2e102014-11-10 15:19:26 -05001385void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001386{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001387 ASSERT(uniformBlockIndex <
1388 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001389
Jamie Madill62d31cb2015-09-11 13:25:51 -04001390 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391
1392 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001393 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001394 case GL_UNIFORM_BLOCK_DATA_SIZE:
1395 *params = static_cast<GLint>(uniformBlock.dataSize);
1396 break;
1397 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001398 *params =
1399 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001400 break;
1401 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1402 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1403 break;
1404 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1405 {
1406 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1407 {
1408 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1409 }
1410 }
1411 break;
1412 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001413 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414 break;
1415 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001416 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417 break;
1418 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001419 }
1420}
1421
Geoff Lange1a27752015-10-05 13:16:04 -04001422GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001423{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424 int maxLength = 0;
1425
1426 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001427 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001428 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001429 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1430 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001431 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432 if (!uniformBlock.name.empty())
1433 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001434 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001435
1436 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001437 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438
1439 maxLength = std::max(length + arrayLength, maxLength);
1440 }
1441 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001442 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001443
1444 return maxLength;
1445}
1446
Geoff Lange1a27752015-10-05 13:16:04 -04001447GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001449 size_t subscript = GL_INVALID_INDEX;
1450 std::string baseName = gl::ParseUniformName(name, &subscript);
1451
1452 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1453 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1454 {
1455 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1456 if (uniformBlock.name == baseName)
1457 {
1458 const bool arrayElementZero =
1459 (subscript == GL_INVALID_INDEX &&
1460 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1461 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1462 {
1463 return blockIndex;
1464 }
1465 }
1466 }
1467
1468 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001469}
1470
Jamie Madill62d31cb2015-09-11 13:25:51 -04001471const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001472{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001473 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1474 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001475}
1476
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001477void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1478{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001479 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001480 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001481}
1482
1483GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1484{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001485 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001486}
1487
1488void Program::resetUniformBlockBindings()
1489{
1490 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1491 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001492 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001493 }
Geoff Lang5d124a62015-09-15 13:03:27 -04001494 mData.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001495}
1496
Geoff Lang48dcae72014-02-05 16:28:24 -05001497void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1498{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001499 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001500 for (GLsizei i = 0; i < count; i++)
1501 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001502 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001503 }
1504
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001505 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001506}
1507
1508void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1509{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001510 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001511 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001512 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1513 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001514 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1515 if (length)
1516 {
1517 *length = lastNameIdx;
1518 }
1519 if (size)
1520 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001521 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001522 }
1523 if (type)
1524 {
1525 *type = varying.type;
1526 }
1527 if (name)
1528 {
1529 memcpy(name, varying.name.c_str(), lastNameIdx);
1530 name[lastNameIdx] = '\0';
1531 }
1532 }
1533}
1534
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001535GLsizei Program::getTransformFeedbackVaryingCount() 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 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001540 }
1541 else
1542 {
1543 return 0;
1544 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001545}
1546
1547GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1548{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001549 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001550 {
1551 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001552 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001553 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001554 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1555 }
1556
1557 return maxSize;
1558 }
1559 else
1560 {
1561 return 0;
1562 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001563}
1564
1565GLenum Program::getTransformFeedbackBufferMode() const
1566{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001567 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001568}
1569
Jamie Madillada9ecc2015-08-17 12:53:37 -04001570// static
1571bool Program::linkVaryings(InfoLog &infoLog,
1572 const Shader *vertexShader,
1573 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001574{
Jamie Madill4cff2472015-08-21 16:53:18 -04001575 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1576 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001577
Jamie Madill4cff2472015-08-21 16:53:18 -04001578 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001579 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001580 bool matched = false;
1581
1582 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001583 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001584 {
1585 continue;
1586 }
1587
Jamie Madill4cff2472015-08-21 16:53:18 -04001588 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001589 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001590 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001591 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001592 ASSERT(!input.isBuiltIn());
1593 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001594 {
1595 return false;
1596 }
1597
Geoff Lang7dd2e102014-11-10 15:19:26 -05001598 matched = true;
1599 break;
1600 }
1601 }
1602
1603 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001604 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001605 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001606 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001607 return false;
1608 }
1609 }
1610
Jamie Madillada9ecc2015-08-17 12:53:37 -04001611 // TODO(jmadill): verify no unmatched vertex varyings?
1612
Geoff Lang7dd2e102014-11-10 15:19:26 -05001613 return true;
1614}
1615
Jamie Madill62d31cb2015-09-11 13:25:51 -04001616bool Program::linkUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Jamie Madillea918db2015-08-18 14:48:59 -04001617{
1618 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1619 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1620
1621 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001622 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001623
1624 for (const sh::Uniform &vertexUniform : vertexUniforms)
1625 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001626 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001627 }
1628
1629 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1630 {
1631 auto entry = linkedUniforms.find(fragmentUniform.name);
1632 if (entry != linkedUniforms.end())
1633 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001634 LinkedUniform *vertexUniform = &entry->second;
1635 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1636 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001637 {
1638 return false;
1639 }
1640 }
1641 }
1642
Jamie Madill62d31cb2015-09-11 13:25:51 -04001643 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1644 // Also check the maximum uniform vector and sampler counts.
1645 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1646 {
1647 return false;
1648 }
1649
1650 indexUniforms();
1651
Jamie Madillea918db2015-08-18 14:48:59 -04001652 return true;
1653}
1654
Jamie Madill62d31cb2015-09-11 13:25:51 -04001655void Program::indexUniforms()
1656{
1657 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1658 {
1659 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1660
1661 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1662 {
1663 if (!uniform.isBuiltIn())
1664 {
1665 // Assign in-order uniform locations
1666 mData.mUniformLocations.push_back(gl::VariableLocation(
1667 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)));
1668 }
1669 }
1670 }
1671}
1672
Geoff Lang7dd2e102014-11-10 15:19:26 -05001673bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1674{
Jamie Madillc4c744222015-11-04 09:39:47 -05001675 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1676 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001677 {
1678 return false;
1679 }
1680
1681 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1682 {
Jamie Madillf6113162015-05-07 11:49:21 -04001683 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001684 return false;
1685 }
1686
1687 return true;
1688}
1689
1690// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001691bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001692 InfoLog &infoLog,
1693 const AttributeBindings &attributeBindings,
1694 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001695{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001696 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001697 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001698 GLuint maxAttribs = data.caps->maxVertexAttributes;
1699
1700 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001701 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001702 {
Jamie Madillf6113162015-05-07 11:49:21 -04001703 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001704 return false;
1705 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001706
Jamie Madillc349ec02015-08-21 16:53:12 -04001707 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001708
Jamie Madillc349ec02015-08-21 16:53:12 -04001709 // Link attributes that have a binding location
1710 for (sh::Attribute &attribute : mData.mAttributes)
1711 {
1712 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001713 ASSERT(attribute.staticUse);
1714
Jamie Madillc349ec02015-08-21 16:53:12 -04001715 int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
1716 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001717 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001718 attribute.location = bindingLocation;
1719 }
1720
1721 if (attribute.location != -1)
1722 {
1723 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001724 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001725
Jamie Madill63805b42015-08-25 13:17:39 -04001726 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001727 {
Jamie Madillf6113162015-05-07 11:49:21 -04001728 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001729 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001730
1731 return false;
1732 }
1733
Jamie Madill63805b42015-08-25 13:17:39 -04001734 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001735 {
Jamie Madill63805b42015-08-25 13:17:39 -04001736 const int regLocation = attribute.location + reg;
1737 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001738
1739 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001740 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001741 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001742 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001743 // TODO(jmadill): fix aliasing on ES2
1744 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001745 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001746 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001747 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001748 return false;
1749 }
1750 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001751 else
1752 {
Jamie Madill63805b42015-08-25 13:17:39 -04001753 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001754 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001755
Jamie Madill63805b42015-08-25 13:17:39 -04001756 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001757 }
1758 }
1759 }
1760
1761 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001762 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001763 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001764 ASSERT(attribute.staticUse);
1765
Jamie Madillc349ec02015-08-21 16:53:12 -04001766 // Not set by glBindAttribLocation or by location layout qualifier
1767 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001768 {
Jamie Madill63805b42015-08-25 13:17:39 -04001769 int regs = VariableRegisterCount(attribute.type);
1770 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001771
Jamie Madill63805b42015-08-25 13:17:39 -04001772 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001773 {
Jamie Madillf6113162015-05-07 11:49:21 -04001774 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001775 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001776 }
1777
Jamie Madillc349ec02015-08-21 16:53:12 -04001778 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001779 }
1780 }
1781
Jamie Madillc349ec02015-08-21 16:53:12 -04001782 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001783 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001784 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001785 ASSERT(attribute.location != -1);
1786 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001787
Jamie Madill63805b42015-08-25 13:17:39 -04001788 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789 {
Jamie Madill63805b42015-08-25 13:17:39 -04001790 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791 }
1792 }
1793
Geoff Lang7dd2e102014-11-10 15:19:26 -05001794 return true;
1795}
1796
Jamie Madille473dee2015-08-18 14:49:01 -04001797bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001798{
Jamie Madille473dee2015-08-18 14:49:01 -04001799 const Shader &vertexShader = *mData.mAttachedVertexShader;
1800 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1801
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1803 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001804
Geoff Lang7dd2e102014-11-10 15:19:26 -05001805 // Check that interface blocks defined in the vertex and fragment shaders are identical
1806 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1807 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001808
1809 GLuint vertexBlockCount = 0;
1810 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001811 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001812 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001813
1814 // Note: shared and std140 layouts are always considered active
1815 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1816 {
1817 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1818 {
1819 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1820 << caps.maxVertexUniformBlocks << ")";
1821 return false;
1822 }
1823 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001824 }
Jamie Madille473dee2015-08-18 14:49:01 -04001825
1826 GLuint fragmentBlockCount = 0;
1827 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001828 {
Jamie Madille473dee2015-08-18 14:49:01 -04001829 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830 if (entry != linkedUniformBlocks.end())
1831 {
1832 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1833 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1834 {
1835 return false;
1836 }
1837 }
Jamie Madille473dee2015-08-18 14:49:01 -04001838
Geoff Lang7dd2e102014-11-10 15:19:26 -05001839 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001840 if (fragmentInterfaceBlock.staticUse ||
1841 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842 {
Jamie Madille473dee2015-08-18 14:49:01 -04001843 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001844 {
Jamie Madille473dee2015-08-18 14:49:01 -04001845 infoLog
1846 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1847 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001848 return false;
1849 }
1850 }
1851 }
Jamie Madille473dee2015-08-18 14:49:01 -04001852
Geoff Lang7dd2e102014-11-10 15:19:26 -05001853 return true;
1854}
1855
1856bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1857 const sh::InterfaceBlock &fragmentInterfaceBlock)
1858{
1859 const char* blockName = vertexInterfaceBlock.name.c_str();
1860 // validate blocks for the same member types
1861 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1862 {
Jamie Madillf6113162015-05-07 11:49:21 -04001863 infoLog << "Types for interface block '" << blockName
1864 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001865 return false;
1866 }
1867 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1868 {
Jamie Madillf6113162015-05-07 11:49:21 -04001869 infoLog << "Array sizes differ for interface block '" << blockName
1870 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871 return false;
1872 }
1873 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
1874 {
Jamie Madillf6113162015-05-07 11:49:21 -04001875 infoLog << "Layout qualifiers differ for interface block '" << blockName
1876 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001877 return false;
1878 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001879 const unsigned int numBlockMembers =
1880 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001881 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1882 {
1883 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
1884 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
1885 if (vertexMember.name != fragmentMember.name)
1886 {
Jamie Madillf6113162015-05-07 11:49:21 -04001887 infoLog << "Name mismatch for field " << blockMemberIndex
1888 << " of interface block '" << blockName
1889 << "': (in vertex: '" << vertexMember.name
1890 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001891 return false;
1892 }
1893 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
1894 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
1895 {
1896 return false;
1897 }
1898 }
1899 return true;
1900}
1901
1902bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
1903 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
1904{
1905 if (vertexVariable.type != fragmentVariable.type)
1906 {
Jamie Madillf6113162015-05-07 11:49:21 -04001907 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001908 return false;
1909 }
1910 if (vertexVariable.arraySize != fragmentVariable.arraySize)
1911 {
Jamie Madillf6113162015-05-07 11:49:21 -04001912 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001913 return false;
1914 }
1915 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
1916 {
Jamie Madillf6113162015-05-07 11:49:21 -04001917 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918 return false;
1919 }
1920
1921 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
1922 {
Jamie Madillf6113162015-05-07 11:49:21 -04001923 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001924 return false;
1925 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001926 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001927 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1928 {
1929 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
1930 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
1931
1932 if (vertexMember.name != fragmentMember.name)
1933 {
Jamie Madillf6113162015-05-07 11:49:21 -04001934 infoLog << "Name mismatch for field '" << memberIndex
1935 << "' of " << variableName
1936 << ": (in vertex: '" << vertexMember.name
1937 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001938 return false;
1939 }
1940
1941 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
1942 vertexMember.name + "'";
1943
1944 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
1945 {
1946 return false;
1947 }
1948 }
1949
1950 return true;
1951}
1952
1953bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
1954{
Cooper Partin1acf4382015-06-12 12:38:57 -07001955#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
1956 const bool validatePrecision = true;
1957#else
1958 const bool validatePrecision = false;
1959#endif
1960
1961 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001962 {
1963 return false;
1964 }
1965
1966 return true;
1967}
1968
1969bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
1970{
1971 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
1972 {
1973 return false;
1974 }
1975
Jamie Madille9cc4692015-02-19 16:00:13 -05001976 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977 {
Jamie Madillf6113162015-05-07 11:49:21 -04001978 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001979 return false;
1980 }
1981
1982 return true;
1983}
1984
Jamie Madillccdf74b2015-08-18 10:46:12 -04001985bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
1986 const std::vector<const sh::Varying *> &varyings,
1987 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988{
1989 size_t totalComponents = 0;
1990
Jamie Madillccdf74b2015-08-18 10:46:12 -04001991 std::set<std::string> uniqueNames;
1992
1993 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001994 {
1995 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001996 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001998 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001999 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002000 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002002 infoLog << "Two transform feedback varyings specify the same output variable ("
2003 << tfVaryingName << ").";
2004 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002005 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002006 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007
Geoff Lang1a683462015-09-29 15:09:59 -04002008 if (varying->isArray())
2009 {
2010 infoLog << "Capture of arrays is undefined and not supported.";
2011 return false;
2012 }
2013
Jamie Madillccdf74b2015-08-18 10:46:12 -04002014 // TODO(jmadill): Investigate implementation limits on D3D11
2015 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04002016 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002017 componentCount > caps.maxTransformFeedbackSeparateComponents)
2018 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002019 infoLog << "Transform feedback varying's " << varying->name << " components ("
2020 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002021 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002022 return false;
2023 }
2024
2025 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026 found = true;
2027 break;
2028 }
2029 }
2030
Jamie Madill89bb70e2015-08-31 14:18:39 -04002031 if (tfVaryingName.find('[') != std::string::npos)
2032 {
Geoff Lang1a683462015-09-29 15:09:59 -04002033 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002034 return false;
2035 }
2036
Geoff Lang7dd2e102014-11-10 15:19:26 -05002037 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2038 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002039 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040 }
2041
Jamie Madillada9ecc2015-08-17 12:53:37 -04002042 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002043 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002044 {
Jamie Madillf6113162015-05-07 11:49:21 -04002045 infoLog << "Transform feedback varying total components (" << totalComponents
2046 << ") exceed the maximum interleaved components ("
2047 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002048 return false;
2049 }
2050
2051 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002052}
2053
Jamie Madillccdf74b2015-08-18 10:46:12 -04002054void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2055{
2056 // Gather the linked varyings that are used for transform feedback, they should all exist.
2057 mData.mTransformFeedbackVaryingVars.clear();
2058 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2059 {
2060 for (const sh::Varying *varying : varyings)
2061 {
2062 if (tfVaryingName == varying->name)
2063 {
2064 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2065 break;
2066 }
2067 }
2068 }
2069}
2070
2071std::vector<const sh::Varying *> Program::getMergedVaryings() const
2072{
2073 std::set<std::string> uniqueNames;
2074 std::vector<const sh::Varying *> varyings;
2075
2076 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2077 {
2078 if (uniqueNames.count(varying.name) == 0)
2079 {
2080 uniqueNames.insert(varying.name);
2081 varyings.push_back(&varying);
2082 }
2083 }
2084
2085 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2086 {
2087 if (uniqueNames.count(varying.name) == 0)
2088 {
2089 uniqueNames.insert(varying.name);
2090 varyings.push_back(&varying);
2091 }
2092 }
2093
2094 return varyings;
2095}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002096
2097void Program::linkOutputVariables()
2098{
2099 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2100 ASSERT(fragmentShader != nullptr);
2101
2102 // Skip this step for GLES2 shaders.
2103 if (fragmentShader->getShaderVersion() == 100)
2104 return;
2105
Jamie Madilla0a9e122015-09-02 15:54:30 -04002106 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002107
2108 // TODO(jmadill): any caps validation here?
2109
2110 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2111 outputVariableIndex++)
2112 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002113 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002114
2115 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2116 if (outputVariable.isBuiltIn())
2117 continue;
2118
2119 // Since multiple output locations must be specified, use 0 for non-specified locations.
2120 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2121
2122 ASSERT(outputVariable.staticUse);
2123
2124 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2125 elementIndex++)
2126 {
2127 const int location = baseLocation + elementIndex;
2128 ASSERT(mData.mOutputVariables.count(location) == 0);
2129 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2130 mData.mOutputVariables[location] =
2131 VariableLocation(outputVariable.name, element, outputVariableIndex);
2132 }
2133 }
2134}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002135
2136bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2137{
2138 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2139 VectorAndSamplerCount vsCounts;
2140
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002141 std::vector<LinkedUniform> samplerUniforms;
2142
Jamie Madill62d31cb2015-09-11 13:25:51 -04002143 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2144 {
2145 if (uniform.staticUse)
2146 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002147 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002148 }
2149 }
2150
2151 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2152 {
2153 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2154 << caps.maxVertexUniformVectors << ").";
2155 return false;
2156 }
2157
2158 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2159 {
2160 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2161 << caps.maxVertexTextureImageUnits << ").";
2162 return false;
2163 }
2164
2165 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2166 VectorAndSamplerCount fsCounts;
2167
2168 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2169 {
2170 if (uniform.staticUse)
2171 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002172 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002173 }
2174 }
2175
2176 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2177 {
2178 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2179 << caps.maxFragmentUniformVectors << ").";
2180 return false;
2181 }
2182
2183 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2184 {
2185 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2186 << caps.maxTextureImageUnits << ").";
2187 return false;
2188 }
2189
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002190 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2191 mSamplerUniformRange.end =
2192 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2193
2194 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2195
Jamie Madill62d31cb2015-09-11 13:25:51 -04002196 return true;
2197}
2198
2199Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002200 const std::string &fullName,
2201 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002202{
2203 VectorAndSamplerCount vectorAndSamplerCount;
2204
2205 if (uniform.isStruct())
2206 {
2207 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2208 {
2209 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2210
2211 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2212 {
2213 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2214 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2215
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002216 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002217 }
2218 }
2219
2220 return vectorAndSamplerCount;
2221 }
2222
2223 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002224 bool isSampler = IsSamplerType(uniform.type);
2225 if (!UniformInList(mData.getUniforms(), fullName) && !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002226 {
2227 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2228 uniform.arraySize, -1,
2229 sh::BlockMemberInfo::getDefaultBlockInfo());
2230 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002231
2232 // Store sampler uniforms separately, so we'll append them to the end of the list.
2233 if (isSampler)
2234 {
2235 samplerUniforms->push_back(linkedUniform);
2236 }
2237 else
2238 {
2239 mData.mUniforms.push_back(linkedUniform);
2240 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002241 }
2242
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002243 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002244
2245 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2246 // Likewise, don't count "real" uniforms towards sampler count.
2247 vectorAndSamplerCount.vectorCount =
2248 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002249 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002250
2251 return vectorAndSamplerCount;
2252}
2253
2254void Program::gatherInterfaceBlockInfo()
2255{
2256 std::set<std::string> visitedList;
2257
2258 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2259
2260 ASSERT(mData.mUniformBlocks.empty());
2261 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2262 {
2263 // Only 'packed' blocks are allowed to be considered inacive.
2264 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2265 continue;
2266
2267 if (visitedList.count(vertexBlock.name) > 0)
2268 continue;
2269
2270 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2271 visitedList.insert(vertexBlock.name);
2272 }
2273
2274 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2275
2276 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2277 {
2278 // Only 'packed' blocks are allowed to be considered inacive.
2279 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2280 continue;
2281
2282 if (visitedList.count(fragmentBlock.name) > 0)
2283 {
2284 for (gl::UniformBlock &block : mData.mUniformBlocks)
2285 {
2286 if (block.name == fragmentBlock.name)
2287 {
2288 block.fragmentStaticUse = fragmentBlock.staticUse;
2289 }
2290 }
2291
2292 continue;
2293 }
2294
2295 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2296 visitedList.insert(fragmentBlock.name);
2297 }
2298}
2299
Jamie Madill4a3c2342015-10-08 12:58:45 -04002300template <typename VarT>
2301void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2302 const std::string &prefix,
2303 int blockIndex)
2304{
2305 for (const VarT &field : fields)
2306 {
2307 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2308
2309 if (field.isStruct())
2310 {
2311 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2312 {
2313 const std::string uniformElementName =
2314 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2315 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2316 }
2317 }
2318 else
2319 {
2320 // If getBlockMemberInfo returns false, the uniform is optimized out.
2321 sh::BlockMemberInfo memberInfo;
2322 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2323 {
2324 continue;
2325 }
2326
2327 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2328 blockIndex, memberInfo);
2329
2330 // Since block uniforms have no location, we don't need to store them in the uniform
2331 // locations list.
2332 mData.mUniforms.push_back(newUniform);
2333 }
2334 }
2335}
2336
Jamie Madill62d31cb2015-09-11 13:25:51 -04002337void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2338{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002339 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2340 size_t blockSize = 0;
2341
2342 // Don't define this block at all if it's not active in the implementation.
2343 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2344 {
2345 return;
2346 }
2347
2348 // Track the first and last uniform index to determine the range of active uniforms in the
2349 // block.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002350 size_t firstBlockUniformIndex = mData.mUniforms.size();
Jamie Madill4a3c2342015-10-08 12:58:45 -04002351 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002352 size_t lastBlockUniformIndex = mData.mUniforms.size();
2353
2354 std::vector<unsigned int> blockUniformIndexes;
2355 for (size_t blockUniformIndex = firstBlockUniformIndex;
2356 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2357 {
2358 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2359 }
2360
2361 if (interfaceBlock.arraySize > 0)
2362 {
2363 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2364 {
2365 UniformBlock block(interfaceBlock.name, true, arrayElement);
2366 block.memberUniformIndexes = blockUniformIndexes;
2367
2368 if (shaderType == GL_VERTEX_SHADER)
2369 {
2370 block.vertexStaticUse = interfaceBlock.staticUse;
2371 }
2372 else
2373 {
2374 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2375 block.fragmentStaticUse = interfaceBlock.staticUse;
2376 }
2377
Jamie Madill4a3c2342015-10-08 12:58:45 -04002378 // TODO(jmadill): Determine if we can ever have an inactive array element block.
2379 size_t blockElementSize = 0;
2380 if (!mProgram->getUniformBlockSize(block.nameWithArrayIndex(), &blockElementSize))
2381 {
2382 continue;
2383 }
2384
2385 ASSERT(blockElementSize == blockSize);
2386 block.dataSize = static_cast<unsigned int>(blockElementSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002387 mData.mUniformBlocks.push_back(block);
2388 }
2389 }
2390 else
2391 {
2392 UniformBlock block(interfaceBlock.name, false, 0);
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 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002406 mData.mUniformBlocks.push_back(block);
2407 }
2408}
2409
2410template <typename T>
2411void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2412{
2413 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2414 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2415 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2416
2417 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2418 {
2419 // Do a cast conversion for boolean types. From the spec:
2420 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2421 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2422 for (GLsizei component = 0; component < count; ++component)
2423 {
2424 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2425 }
2426 }
2427 else
2428 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002429 // Invalide the validation cache if we modify the sampler data.
2430 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2431 {
2432 mCachedValidateSamplersResult.reset();
2433 }
2434
Jamie Madill62d31cb2015-09-11 13:25:51 -04002435 memcpy(destPointer, v, sizeof(T) * count);
2436 }
2437}
2438
2439template <size_t cols, size_t rows, typename T>
2440void Program::setMatrixUniformInternal(GLint location,
2441 GLsizei count,
2442 GLboolean transpose,
2443 const T *v)
2444{
2445 if (!transpose)
2446 {
2447 setUniformInternal(location, count * cols * rows, v);
2448 return;
2449 }
2450
2451 // Perform a transposing copy.
2452 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2453 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2454 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2455 for (GLsizei element = 0; element < count; ++element)
2456 {
2457 size_t elementOffset = element * rows * cols;
2458
2459 for (size_t row = 0; row < rows; ++row)
2460 {
2461 for (size_t col = 0; col < cols; ++col)
2462 {
2463 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2464 }
2465 }
2466 }
2467}
2468
2469template <typename DestT>
2470void Program::getUniformInternal(GLint location, DestT *dataOut) const
2471{
2472 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2473 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2474
2475 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2476
2477 GLenum componentType = VariableComponentType(uniform.type);
2478 if (componentType == GLTypeToGLenum<DestT>::value)
2479 {
2480 memcpy(dataOut, srcPointer, uniform.getElementSize());
2481 return;
2482 }
2483
2484 int components = VariableComponentCount(uniform.type) * uniform.elementCount();
2485
2486 switch (componentType)
2487 {
2488 case GL_INT:
2489 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2490 break;
2491 case GL_UNSIGNED_INT:
2492 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2493 break;
2494 case GL_BOOL:
2495 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2496 break;
2497 case GL_FLOAT:
2498 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2499 break;
2500 default:
2501 UNREACHABLE();
2502 }
2503}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504}