blob: 814deb918a6a25dcab4e837652e87212d45b0db0 [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
225LinkedVarying::LinkedVarying()
226{
227}
228
229LinkedVarying::LinkedVarying(const std::string &name, GLenum type, GLsizei size, const std::string &semanticName,
230 unsigned int semanticIndex, unsigned int semanticIndexCount)
231 : name(name), type(type), size(size), semanticName(semanticName), semanticIndex(semanticIndex), semanticIndexCount(semanticIndexCount)
232{
233}
234
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400235Program::Data::Data()
236 : mAttachedFragmentShader(nullptr),
237 mAttachedVertexShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400238 mTransformFeedbackBufferMode(GL_NONE)
239{
240}
241
242Program::Data::~Data()
243{
244 if (mAttachedVertexShader != nullptr)
245 {
246 mAttachedVertexShader->release();
247 }
248
249 if (mAttachedFragmentShader != nullptr)
250 {
251 mAttachedFragmentShader->release();
252 }
253}
254
Jamie Madill62d31cb2015-09-11 13:25:51 -0400255const LinkedUniform *Program::Data::getUniformByName(const std::string &name) const
256{
257 for (const LinkedUniform &linkedUniform : mUniforms)
258 {
259 if (linkedUniform.name == name)
260 {
261 return &linkedUniform;
262 }
263 }
264
265 return nullptr;
266}
267
268GLint Program::Data::getUniformLocation(const std::string &name) const
269{
270 size_t subscript = GL_INVALID_INDEX;
271 std::string baseName = gl::ParseUniformName(name, &subscript);
272
273 for (size_t location = 0; location < mUniformLocations.size(); ++location)
274 {
275 const VariableLocation &uniformLocation = mUniformLocations[location];
276 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
277
278 if (uniform.name == baseName)
279 {
280 if ((uniform.isArray() && uniformLocation.element == subscript) ||
281 (subscript == GL_INVALID_INDEX))
282 {
283 return static_cast<GLint>(location);
284 }
285 }
286 }
287
288 return -1;
289}
290
291GLuint Program::Data::getUniformIndex(const std::string &name) const
292{
293 size_t subscript = GL_INVALID_INDEX;
294 std::string baseName = gl::ParseUniformName(name, &subscript);
295
296 // The app is not allowed to specify array indices other than 0 for arrays of basic types
297 if (subscript != 0 && subscript != GL_INVALID_INDEX)
298 {
299 return GL_INVALID_INDEX;
300 }
301
302 for (size_t index = 0; index < mUniforms.size(); index++)
303 {
304 const LinkedUniform &uniform = mUniforms[index];
305 if (uniform.name == baseName)
306 {
307 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
308 {
309 return static_cast<GLuint>(index);
310 }
311 }
312 }
313
314 return GL_INVALID_INDEX;
315}
316
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400317Program::Program(rx::ImplFactory *factory, ResourceManager *manager, GLuint handle)
318 : mProgram(factory->createProgram(mData)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400319 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500320 mLinked(false),
321 mDeleteStatus(false),
322 mRefCount(0),
323 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400324 mHandle(handle),
325 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500326{
327 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000328
329 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500330 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331}
332
333Program::~Program()
334{
335 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000336
Geoff Lang7dd2e102014-11-10 15:19:26 -0500337 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338}
339
340bool Program::attachShader(Shader *shader)
341{
342 if (shader->getType() == GL_VERTEX_SHADER)
343 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400344 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345 {
346 return false;
347 }
348
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400349 mData.mAttachedVertexShader = shader;
350 mData.mAttachedVertexShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351 }
352 else if (shader->getType() == GL_FRAGMENT_SHADER)
353 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400354 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355 {
356 return false;
357 }
358
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400359 mData.mAttachedFragmentShader = shader;
360 mData.mAttachedFragmentShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361 }
362 else UNREACHABLE();
363
364 return true;
365}
366
367bool Program::detachShader(Shader *shader)
368{
369 if (shader->getType() == GL_VERTEX_SHADER)
370 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400371 if (mData.mAttachedVertexShader != 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.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000378 }
379 else if (shader->getType() == GL_FRAGMENT_SHADER)
380 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400381 if (mData.mAttachedFragmentShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382 {
383 return false;
384 }
385
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400386 shader->release();
387 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388 }
389 else UNREACHABLE();
390
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391 return true;
392}
393
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000394int Program::getAttachedShadersCount() const
395{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400396 return (mData.mAttachedVertexShader ? 1 : 0) + (mData.mAttachedFragmentShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000397}
398
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000399void AttributeBindings::bindAttributeLocation(GLuint index, const char *name)
400{
401 if (index < MAX_VERTEX_ATTRIBS)
402 {
403 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
404 {
405 mAttributeBinding[i].erase(name);
406 }
407
408 mAttributeBinding[index].insert(name);
409 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000410}
411
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412void Program::bindAttributeLocation(GLuint index, const char *name)
413{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000414 mAttributeBindings.bindAttributeLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415}
416
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
418// compiling them into binaries, determining the attribute mappings, and collecting
419// a list of uniforms
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400420Error Program::link(const gl::Data &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000421{
422 unlink(false);
423
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000424 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000425 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000426
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400427 if (!mData.mAttachedFragmentShader || !mData.mAttachedFragmentShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500428 {
429 return Error(GL_NO_ERROR);
430 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400431 ASSERT(mData.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500432
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400433 if (!mData.mAttachedVertexShader || !mData.mAttachedVertexShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500434 {
435 return Error(GL_NO_ERROR);
436 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400437 ASSERT(mData.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500438
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400439 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mData.mAttachedVertexShader))
Jamie Madill437d2662014-12-05 14:23:35 -0500440 {
441 return Error(GL_NO_ERROR);
442 }
443
Jamie Madillada9ecc2015-08-17 12:53:37 -0400444 if (!linkVaryings(mInfoLog, mData.mAttachedVertexShader, mData.mAttachedFragmentShader))
445 {
446 return Error(GL_NO_ERROR);
447 }
448
Jamie Madillea918db2015-08-18 14:48:59 -0400449 if (!linkUniforms(mInfoLog, *data.caps))
450 {
451 return Error(GL_NO_ERROR);
452 }
453
Jamie Madille473dee2015-08-18 14:49:01 -0400454 if (!linkUniformBlocks(mInfoLog, *data.caps))
455 {
456 return Error(GL_NO_ERROR);
457 }
458
Jamie Madillccdf74b2015-08-18 10:46:12 -0400459 const auto &mergedVaryings = getMergedVaryings();
460
461 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, *data.caps))
462 {
463 return Error(GL_NO_ERROR);
464 }
465
Jamie Madill80a6fc02015-08-21 16:53:16 -0400466 linkOutputVariables();
467
Jamie Madillf5f4ad22015-09-02 18:32:38 +0000468 rx::LinkResult result = mProgram->link(data, mInfoLog);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500469 if (result.error.isError() || !result.linkSuccess)
470 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500471 return result.error;
472 }
473
Jamie Madillccdf74b2015-08-18 10:46:12 -0400474 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400475 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400476
Geoff Lang7dd2e102014-11-10 15:19:26 -0500477 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400478 return gl::Error(GL_NO_ERROR);
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000479}
480
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000481int AttributeBindings::getAttributeBinding(const std::string &name) const
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000482{
483 for (int location = 0; location < MAX_VERTEX_ATTRIBS; location++)
484 {
485 if (mAttributeBinding[location].find(name) != mAttributeBinding[location].end())
486 {
487 return location;
488 }
489 }
490
491 return -1;
492}
493
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000494// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495void Program::unlink(bool destroy)
496{
497 if (destroy) // Object being destructed
498 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400499 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400501 mData.mAttachedFragmentShader->release();
502 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503 }
504
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400505 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400507 mData.mAttachedVertexShader->release();
508 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510 }
511
Jamie Madillc349ec02015-08-21 16:53:12 -0400512 mData.mAttributes.clear();
Jamie Madill63805b42015-08-25 13:17:39 -0400513 mData.mActiveAttribLocationsMask.reset();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400514 mData.mTransformFeedbackVaryingVars.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400515 mData.mUniforms.clear();
516 mData.mUniformLocations.clear();
517 mData.mUniformBlocks.clear();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400518 mData.mOutputVariables.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500519
Geoff Lang7dd2e102014-11-10 15:19:26 -0500520 mValidated = false;
521
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000522 mLinked = false;
523}
524
Geoff Lange1a27752015-10-05 13:16:04 -0400525bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000526{
527 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000528}
529
Geoff Lang7dd2e102014-11-10 15:19:26 -0500530Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000531{
532 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000533
Geoff Lang7dd2e102014-11-10 15:19:26 -0500534#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
535 return Error(GL_NO_ERROR);
536#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400537 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
538 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000539 {
Jamie Madillf6113162015-05-07 11:49:21 -0400540 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500541 return Error(GL_NO_ERROR);
542 }
543
Geoff Langc46cc2f2015-10-01 17:16:20 -0400544 BinaryInputStream stream(binary, length);
545
Geoff Lang7dd2e102014-11-10 15:19:26 -0500546 int majorVersion = stream.readInt<int>();
547 int minorVersion = stream.readInt<int>();
548 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
549 {
Jamie Madillf6113162015-05-07 11:49:21 -0400550 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500551 return Error(GL_NO_ERROR);
552 }
553
554 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
555 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
556 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
557 {
Jamie Madillf6113162015-05-07 11:49:21 -0400558 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500559 return Error(GL_NO_ERROR);
560 }
561
Jamie Madill63805b42015-08-25 13:17:39 -0400562 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
563 "Too many vertex attribs for mask");
564 mData.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500565
Jamie Madill3da79b72015-04-27 11:09:17 -0400566 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400567 ASSERT(mData.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400568 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
569 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400570 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400571 LoadShaderVar(&stream, &attrib);
572 attrib.location = stream.readInt<int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400573 mData.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400574 }
575
Jamie Madill62d31cb2015-09-11 13:25:51 -0400576 unsigned int uniformCount = stream.readInt<unsigned int>();
577 ASSERT(mData.mUniforms.empty());
578 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
579 {
580 LinkedUniform uniform;
581 LoadShaderVar(&stream, &uniform);
582
583 uniform.blockIndex = stream.readInt<int>();
584 uniform.blockInfo.offset = stream.readInt<int>();
585 uniform.blockInfo.arrayStride = stream.readInt<int>();
586 uniform.blockInfo.matrixStride = stream.readInt<int>();
587 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
588
589 mData.mUniforms.push_back(uniform);
590 }
591
592 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
593 ASSERT(mData.mUniformLocations.empty());
594 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
595 uniformIndexIndex++)
596 {
597 VariableLocation variable;
598 stream.readString(&variable.name);
599 stream.readInt(&variable.element);
600 stream.readInt(&variable.index);
601
602 mData.mUniformLocations.push_back(variable);
603 }
604
605 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
606 ASSERT(mData.mUniformBlocks.empty());
607 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
608 ++uniformBlockIndex)
609 {
610 UniformBlock uniformBlock;
611 stream.readString(&uniformBlock.name);
612 stream.readBool(&uniformBlock.isArray);
613 stream.readInt(&uniformBlock.arrayElement);
614 stream.readInt(&uniformBlock.dataSize);
615 stream.readBool(&uniformBlock.vertexStaticUse);
616 stream.readBool(&uniformBlock.fragmentStaticUse);
617
618 unsigned int numMembers = stream.readInt<unsigned int>();
619 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
620 {
621 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
622 }
623
Jamie Madill62d31cb2015-09-11 13:25:51 -0400624 mData.mUniformBlocks.push_back(uniformBlock);
625 }
626
Brandon Jones1048ea72015-10-06 15:34:52 -0700627 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
628 ASSERT(mData.mTransformFeedbackVaryingVars.empty());
629 for (unsigned int transformFeedbackVaryingIndex = 0;
630 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
631 ++transformFeedbackVaryingIndex)
632 {
633 sh::Varying varying;
634 stream.readInt(&varying.arraySize);
635 stream.readInt(&varying.type);
636 stream.readString(&varying.name);
637
638 mData.mTransformFeedbackVaryingVars.push_back(varying);
639 }
640
Jamie Madillada9ecc2015-08-17 12:53:37 -0400641 stream.readInt(&mData.mTransformFeedbackBufferMode);
642
Jamie Madill80a6fc02015-08-21 16:53:16 -0400643 unsigned int outputVarCount = stream.readInt<unsigned int>();
644 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
645 {
646 int locationIndex = stream.readInt<int>();
647 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400648 stream.readInt(&locationData.element);
649 stream.readInt(&locationData.index);
650 stream.readString(&locationData.name);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400651 mData.mOutputVariables[locationIndex] = locationData;
652 }
653
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400654 stream.readInt(&mSamplerUniformRange.start);
655 stream.readInt(&mSamplerUniformRange.end);
656
Geoff Lang7dd2e102014-11-10 15:19:26 -0500657 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
658 if (result.error.isError() || !result.linkSuccess)
659 {
Geoff Langb543aff2014-09-30 14:52:54 -0400660 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000661 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000662
Geoff Lang7dd2e102014-11-10 15:19:26 -0500663 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400664 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500665#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
666}
667
668Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
669{
670 if (binaryFormat)
671 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400672 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500673 }
674
675 BinaryOutputStream stream;
676
Geoff Lang7dd2e102014-11-10 15:19:26 -0500677 stream.writeInt(ANGLE_MAJOR_VERSION);
678 stream.writeInt(ANGLE_MINOR_VERSION);
679 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
680
Jamie Madill63805b42015-08-25 13:17:39 -0400681 stream.writeInt(mData.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500682
Jamie Madillc349ec02015-08-21 16:53:12 -0400683 stream.writeInt(mData.mAttributes.size());
684 for (const sh::Attribute &attrib : mData.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400685 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400686 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400687 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400688 }
689
690 stream.writeInt(mData.mUniforms.size());
691 for (const gl::LinkedUniform &uniform : mData.mUniforms)
692 {
693 WriteShaderVar(&stream, uniform);
694
695 // FIXME: referenced
696
697 stream.writeInt(uniform.blockIndex);
698 stream.writeInt(uniform.blockInfo.offset);
699 stream.writeInt(uniform.blockInfo.arrayStride);
700 stream.writeInt(uniform.blockInfo.matrixStride);
701 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
702 }
703
704 stream.writeInt(mData.mUniformLocations.size());
705 for (const auto &variable : mData.mUniformLocations)
706 {
707 stream.writeString(variable.name);
708 stream.writeInt(variable.element);
709 stream.writeInt(variable.index);
710 }
711
712 stream.writeInt(mData.mUniformBlocks.size());
713 for (const UniformBlock &uniformBlock : mData.mUniformBlocks)
714 {
715 stream.writeString(uniformBlock.name);
716 stream.writeInt(uniformBlock.isArray);
717 stream.writeInt(uniformBlock.arrayElement);
718 stream.writeInt(uniformBlock.dataSize);
719
720 stream.writeInt(uniformBlock.vertexStaticUse);
721 stream.writeInt(uniformBlock.fragmentStaticUse);
722
723 stream.writeInt(uniformBlock.memberUniformIndexes.size());
724 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
725 {
726 stream.writeInt(memberUniformIndex);
727 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400728 }
729
Brandon Jones1048ea72015-10-06 15:34:52 -0700730 stream.writeInt(mData.mTransformFeedbackVaryingVars.size());
731 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
732 {
733 stream.writeInt(varying.arraySize);
734 stream.writeInt(varying.type);
735 stream.writeString(varying.name);
736 }
737
Jamie Madillada9ecc2015-08-17 12:53:37 -0400738 stream.writeInt(mData.mTransformFeedbackBufferMode);
739
Jamie Madill80a6fc02015-08-21 16:53:16 -0400740 stream.writeInt(mData.mOutputVariables.size());
741 for (const auto &outputPair : mData.mOutputVariables)
742 {
743 stream.writeInt(outputPair.first);
744 stream.writeInt(outputPair.second.element);
745 stream.writeInt(outputPair.second.index);
746 stream.writeString(outputPair.second.name);
747 }
748
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400749 stream.writeInt(mSamplerUniformRange.start);
750 stream.writeInt(mSamplerUniformRange.end);
751
Geoff Lang7dd2e102014-11-10 15:19:26 -0500752 gl::Error error = mProgram->save(&stream);
753 if (error.isError())
754 {
755 return error;
756 }
757
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700758 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500759 const void *streamData = stream.data();
760
761 if (streamLength > bufSize)
762 {
763 if (length)
764 {
765 *length = 0;
766 }
767
768 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
769 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
770 // sizes and then copy it.
771 return Error(GL_INVALID_OPERATION);
772 }
773
774 if (binary)
775 {
776 char *ptr = reinterpret_cast<char*>(binary);
777
778 memcpy(ptr, streamData, streamLength);
779 ptr += streamLength;
780
781 ASSERT(ptr - streamLength == binary);
782 }
783
784 if (length)
785 {
786 *length = streamLength;
787 }
788
789 return Error(GL_NO_ERROR);
790}
791
792GLint Program::getBinaryLength() const
793{
794 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400795 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500796 if (error.isError())
797 {
798 return 0;
799 }
800
801 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000802}
803
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000804void Program::release()
805{
806 mRefCount--;
807
808 if (mRefCount == 0 && mDeleteStatus)
809 {
810 mResourceManager->deleteProgram(mHandle);
811 }
812}
813
814void Program::addRef()
815{
816 mRefCount++;
817}
818
819unsigned int Program::getRefCount() const
820{
821 return mRefCount;
822}
823
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000824int Program::getInfoLogLength() const
825{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400826 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000827}
828
Geoff Lange1a27752015-10-05 13:16:04 -0400829void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000830{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000831 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000832}
833
Geoff Lange1a27752015-10-05 13:16:04 -0400834void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000835{
836 int total = 0;
837
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400838 if (mData.mAttachedVertexShader)
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.mAttachedVertexShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000843 }
844
845 total++;
846 }
847
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400848 if (mData.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000849 {
850 if (total < maxCount)
851 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400852 shaders[total] = mData.mAttachedFragmentShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000853 }
854
855 total++;
856 }
857
858 if (count)
859 {
860 *count = total;
861 }
862}
863
Geoff Lange1a27752015-10-05 13:16:04 -0400864GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500865{
Jamie Madillc349ec02015-08-21 16:53:12 -0400866 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500867 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400868 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500869 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400870 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500871 }
872 }
873
Austin Kinrossb8af7232015-03-16 22:33:25 -0700874 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500875}
876
Jamie Madill63805b42015-08-25 13:17:39 -0400877bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400878{
Olli Etuaho401d9fe2015-08-26 10:19:59 +0300879 ASSERT(attribLocation < mData.mActiveAttribLocationsMask.size());
Jamie Madill63805b42015-08-25 13:17:39 -0400880 return mData.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500881}
882
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000883void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
884{
Jamie Madillc349ec02015-08-21 16:53:12 -0400885 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000886 {
887 if (bufsize > 0)
888 {
889 name[0] = '\0';
890 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500891
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000892 if (length)
893 {
894 *length = 0;
895 }
896
897 *type = GL_NONE;
898 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400899 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000900 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400901
902 size_t attributeIndex = 0;
903
904 for (const sh::Attribute &attribute : mData.mAttributes)
905 {
906 // Skip over inactive attributes
907 if (attribute.staticUse)
908 {
909 if (static_cast<size_t>(index) == attributeIndex)
910 {
911 break;
912 }
913 attributeIndex++;
914 }
915 }
916
917 ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
918 const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
919
920 if (bufsize > 0)
921 {
922 const char *string = attrib.name.c_str();
923
924 strncpy(name, string, bufsize);
925 name[bufsize - 1] = '\0';
926
927 if (length)
928 {
929 *length = static_cast<GLsizei>(strlen(name));
930 }
931 }
932
933 // Always a single 'type' instance
934 *size = 1;
935 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000936}
937
Geoff Lange1a27752015-10-05 13:16:04 -0400938GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000939{
Jamie Madillc349ec02015-08-21 16:53:12 -0400940 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400941 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400942 return 0;
943 }
944
945 GLint count = 0;
946
947 for (const sh::Attribute &attrib : mData.mAttributes)
948 {
949 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000950 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500951
952 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000953}
954
Geoff Lange1a27752015-10-05 13:16:04 -0400955GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000956{
Jamie Madillc349ec02015-08-21 16:53:12 -0400957 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400958 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400959 return 0;
960 }
961
962 size_t maxLength = 0;
963
964 for (const sh::Attribute &attrib : mData.mAttributes)
965 {
966 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500967 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400968 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500969 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000970 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500971
Jamie Madillc349ec02015-08-21 16:53:12 -0400972 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500973}
974
Geoff Lang7dd2e102014-11-10 15:19:26 -0500975GLint Program::getFragDataLocation(const std::string &name) const
976{
977 std::string baseName(name);
978 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400979 for (auto outputPair : mData.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000980 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400981 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500982 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
983 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400984 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500985 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000986 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500987 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000988}
989
Geoff Lange1a27752015-10-05 13:16:04 -0400990void Program::getActiveUniform(GLuint index,
991 GLsizei bufsize,
992 GLsizei *length,
993 GLint *size,
994 GLenum *type,
995 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000996{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500997 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000998 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400999 // index must be smaller than getActiveUniformCount()
1000 ASSERT(index < mData.mUniforms.size());
1001 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001002
1003 if (bufsize > 0)
1004 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001005 std::string string = uniform.name;
1006 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001007 {
1008 string += "[0]";
1009 }
1010
1011 strncpy(name, string.c_str(), bufsize);
1012 name[bufsize - 1] = '\0';
1013
1014 if (length)
1015 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001016 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001017 }
1018 }
1019
Jamie Madill62d31cb2015-09-11 13:25:51 -04001020 *size = uniform.elementCount();
1021 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001022 }
1023 else
1024 {
1025 if (bufsize > 0)
1026 {
1027 name[0] = '\0';
1028 }
1029
1030 if (length)
1031 {
1032 *length = 0;
1033 }
1034
1035 *size = 0;
1036 *type = GL_NONE;
1037 }
1038}
1039
Geoff Lange1a27752015-10-05 13:16:04 -04001040GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001041{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001042 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001043 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001044 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001045 }
1046 else
1047 {
1048 return 0;
1049 }
1050}
1051
Geoff Lange1a27752015-10-05 13:16:04 -04001052GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001053{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001054 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001055
1056 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001057 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001058 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001059 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001060 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001061 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001062 size_t length = uniform.name.length() + 1u;
1063 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001064 {
1065 length += 3; // Counting in "[0]".
1066 }
1067 maxLength = std::max(length, maxLength);
1068 }
1069 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001070 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001071
Jamie Madill62d31cb2015-09-11 13:25:51 -04001072 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001073}
1074
1075GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1076{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001077 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1078 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001079 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001080 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1082 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1083 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1084 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1085 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1086 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1087 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1088 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1089 default:
1090 UNREACHABLE();
1091 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001092 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001093 return 0;
1094}
1095
1096bool Program::isValidUniformLocation(GLint location) const
1097{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001098 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
1099 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001100}
1101
Jamie Madill62d31cb2015-09-11 13:25:51 -04001102const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001103{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001104 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1105 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106}
1107
Jamie Madill62d31cb2015-09-11 13:25:51 -04001108GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001109{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001110 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001111}
1112
Jamie Madill62d31cb2015-09-11 13:25:51 -04001113GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001114{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001115 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001116}
1117
1118void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1119{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001120 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001121 mProgram->setUniform1fv(location, count, v);
1122}
1123
1124void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1125{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001126 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001127 mProgram->setUniform2fv(location, count, v);
1128}
1129
1130void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1131{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001132 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001133 mProgram->setUniform3fv(location, count, v);
1134}
1135
1136void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1137{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001138 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001139 mProgram->setUniform4fv(location, count, v);
1140}
1141
1142void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1143{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001144 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001145 mProgram->setUniform1iv(location, count, v);
1146}
1147
1148void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1149{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001150 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001151 mProgram->setUniform2iv(location, count, v);
1152}
1153
1154void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1155{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001156 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001157 mProgram->setUniform3iv(location, count, v);
1158}
1159
1160void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1161{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001162 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001163 mProgram->setUniform4iv(location, count, v);
1164}
1165
1166void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1167{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001168 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169 mProgram->setUniform1uiv(location, count, v);
1170}
1171
1172void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1173{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001174 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001175 mProgram->setUniform2uiv(location, count, v);
1176}
1177
1178void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1179{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001180 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001181 mProgram->setUniform3uiv(location, count, v);
1182}
1183
1184void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1185{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001186 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001187 mProgram->setUniform4uiv(location, count, v);
1188}
1189
1190void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1191{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001192 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001193 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1194}
1195
1196void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1197{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001198 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001199 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1200}
1201
1202void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1203{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001204 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001205 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1206}
1207
1208void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1209{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001210 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1212}
1213
1214void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1215{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001216 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001217 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1218}
1219
1220void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1221{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001222 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1224}
1225
1226void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1227{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001228 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001229 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1230}
1231
1232void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1233{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001234 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001235 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1236}
1237
1238void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1239{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001240 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001241 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1242}
1243
Geoff Lange1a27752015-10-05 13:16:04 -04001244void Program::getUniformfv(GLint location, GLfloat *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
Geoff Lange1a27752015-10-05 13:16:04 -04001249void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001250{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001251 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001252}
1253
Geoff Lange1a27752015-10-05 13:16:04 -04001254void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001255{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001256 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001257}
1258
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001259void Program::flagForDeletion()
1260{
1261 mDeleteStatus = true;
1262}
1263
1264bool Program::isFlaggedForDeletion() const
1265{
1266 return mDeleteStatus;
1267}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001268
Brandon Jones43a53e22014-08-28 16:23:22 -07001269void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001270{
1271 mInfoLog.reset();
1272
Geoff Lang7dd2e102014-11-10 15:19:26 -05001273 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001274 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001275 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001276 }
1277 else
1278 {
Jamie Madillf6113162015-05-07 11:49:21 -04001279 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001280 }
1281}
1282
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1284{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001285 // Skip cache if we're using an infolog, so we get the full error.
1286 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1287 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1288 {
1289 return mCachedValidateSamplersResult.value();
1290 }
1291
1292 if (mTextureUnitTypesCache.empty())
1293 {
1294 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1295 }
1296 else
1297 {
1298 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1299 }
1300
1301 // if any two active samplers in a program are of different types, but refer to the same
1302 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1303 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1304 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1305 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1306 {
1307 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1308 ASSERT(uniform.isSampler());
1309
1310 if (!uniform.staticUse)
1311 continue;
1312
1313 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1314 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1315
1316 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1317 {
1318 GLuint textureUnit = dataPtr[arrayElement];
1319
1320 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1321 {
1322 if (infoLog)
1323 {
1324 (*infoLog) << "Sampler uniform (" << textureUnit
1325 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1326 << caps.maxCombinedTextureImageUnits << ")";
1327 }
1328
1329 mCachedValidateSamplersResult = false;
1330 return false;
1331 }
1332
1333 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1334 {
1335 if (textureType != mTextureUnitTypesCache[textureUnit])
1336 {
1337 if (infoLog)
1338 {
1339 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1340 "image unit ("
1341 << textureUnit << ").";
1342 }
1343
1344 mCachedValidateSamplersResult = false;
1345 return false;
1346 }
1347 }
1348 else
1349 {
1350 mTextureUnitTypesCache[textureUnit] = textureType;
1351 }
1352 }
1353 }
1354
1355 mCachedValidateSamplersResult = true;
1356 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357}
1358
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001359bool Program::isValidated() const
1360{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361 return mValidated;
1362}
1363
Geoff Lange1a27752015-10-05 13:16:04 -04001364GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001365{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001366 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001367}
1368
1369void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1370{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001371 ASSERT(uniformBlockIndex <
1372 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373
Jamie Madill62d31cb2015-09-11 13:25:51 -04001374 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001375
1376 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001377 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378 std::string string = uniformBlock.name;
1379
Jamie Madill62d31cb2015-09-11 13:25:51 -04001380 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001381 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001382 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001383 }
1384
1385 strncpy(uniformBlockName, string.c_str(), bufSize);
1386 uniformBlockName[bufSize - 1] = '\0';
1387
1388 if (length)
1389 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001390 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001392 }
1393}
1394
Geoff Lang7dd2e102014-11-10 15:19:26 -05001395void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001396{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001397 ASSERT(uniformBlockIndex <
1398 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001399
Jamie Madill62d31cb2015-09-11 13:25:51 -04001400 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401
1402 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001403 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001404 case GL_UNIFORM_BLOCK_DATA_SIZE:
1405 *params = static_cast<GLint>(uniformBlock.dataSize);
1406 break;
1407 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001408 *params =
1409 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001410 break;
1411 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1412 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1413 break;
1414 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1415 {
1416 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1417 {
1418 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1419 }
1420 }
1421 break;
1422 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001423 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001424 break;
1425 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001426 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427 break;
1428 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001429 }
1430}
1431
Geoff Lange1a27752015-10-05 13:16:04 -04001432GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001433{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434 int maxLength = 0;
1435
1436 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001437 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001438 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1440 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001441 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001442 if (!uniformBlock.name.empty())
1443 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001444 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445
1446 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001447 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448
1449 maxLength = std::max(length + arrayLength, maxLength);
1450 }
1451 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001452 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001453
1454 return maxLength;
1455}
1456
Geoff Lange1a27752015-10-05 13:16:04 -04001457GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001458{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001459 size_t subscript = GL_INVALID_INDEX;
1460 std::string baseName = gl::ParseUniformName(name, &subscript);
1461
1462 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1463 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1464 {
1465 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1466 if (uniformBlock.name == baseName)
1467 {
1468 const bool arrayElementZero =
1469 (subscript == GL_INVALID_INDEX &&
1470 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1471 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1472 {
1473 return blockIndex;
1474 }
1475 }
1476 }
1477
1478 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001479}
1480
Jamie Madill62d31cb2015-09-11 13:25:51 -04001481const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001482{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001483 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1484 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001485}
1486
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001487void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1488{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001489 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001490 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001491}
1492
1493GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1494{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001495 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001496}
1497
1498void Program::resetUniformBlockBindings()
1499{
1500 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1501 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001502 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001503 }
Geoff Lang5d124a62015-09-15 13:03:27 -04001504 mData.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001505}
1506
Geoff Lang48dcae72014-02-05 16:28:24 -05001507void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1508{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001509 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001510 for (GLsizei i = 0; i < count; i++)
1511 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001512 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001513 }
1514
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001515 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001516}
1517
1518void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1519{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001520 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001521 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001522 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1523 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001524 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1525 if (length)
1526 {
1527 *length = lastNameIdx;
1528 }
1529 if (size)
1530 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001531 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001532 }
1533 if (type)
1534 {
1535 *type = varying.type;
1536 }
1537 if (name)
1538 {
1539 memcpy(name, varying.name.c_str(), lastNameIdx);
1540 name[lastNameIdx] = '\0';
1541 }
1542 }
1543}
1544
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001545GLsizei Program::getTransformFeedbackVaryingCount() const
1546{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001547 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001548 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001549 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001550 }
1551 else
1552 {
1553 return 0;
1554 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001555}
1556
1557GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1558{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001559 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001560 {
1561 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001562 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001563 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001564 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1565 }
1566
1567 return maxSize;
1568 }
1569 else
1570 {
1571 return 0;
1572 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001573}
1574
1575GLenum Program::getTransformFeedbackBufferMode() const
1576{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001577 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001578}
1579
Jamie Madillada9ecc2015-08-17 12:53:37 -04001580// static
1581bool Program::linkVaryings(InfoLog &infoLog,
1582 const Shader *vertexShader,
1583 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001584{
Jamie Madill4cff2472015-08-21 16:53:18 -04001585 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1586 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001587
Jamie Madill4cff2472015-08-21 16:53:18 -04001588 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001589 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001590 bool matched = false;
1591
1592 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001593 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001594 {
1595 continue;
1596 }
1597
Jamie Madill4cff2472015-08-21 16:53:18 -04001598 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001599 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001600 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001601 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001602 ASSERT(!input.isBuiltIn());
1603 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001604 {
1605 return false;
1606 }
1607
Geoff Lang7dd2e102014-11-10 15:19:26 -05001608 matched = true;
1609 break;
1610 }
1611 }
1612
1613 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001614 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001615 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001616 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001617 return false;
1618 }
1619 }
1620
Jamie Madillada9ecc2015-08-17 12:53:37 -04001621 // TODO(jmadill): verify no unmatched vertex varyings?
1622
Geoff Lang7dd2e102014-11-10 15:19:26 -05001623 return true;
1624}
1625
Jamie Madill62d31cb2015-09-11 13:25:51 -04001626bool Program::linkUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Jamie Madillea918db2015-08-18 14:48:59 -04001627{
1628 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1629 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1630
1631 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001632 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001633
1634 for (const sh::Uniform &vertexUniform : vertexUniforms)
1635 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001636 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001637 }
1638
1639 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1640 {
1641 auto entry = linkedUniforms.find(fragmentUniform.name);
1642 if (entry != linkedUniforms.end())
1643 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001644 LinkedUniform *vertexUniform = &entry->second;
1645 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1646 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001647 {
1648 return false;
1649 }
1650 }
1651 }
1652
Jamie Madill62d31cb2015-09-11 13:25:51 -04001653 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1654 // Also check the maximum uniform vector and sampler counts.
1655 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1656 {
1657 return false;
1658 }
1659
1660 indexUniforms();
1661
Jamie Madillea918db2015-08-18 14:48:59 -04001662 return true;
1663}
1664
Jamie Madill62d31cb2015-09-11 13:25:51 -04001665void Program::indexUniforms()
1666{
1667 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1668 {
1669 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1670
1671 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1672 {
1673 if (!uniform.isBuiltIn())
1674 {
1675 // Assign in-order uniform locations
1676 mData.mUniformLocations.push_back(gl::VariableLocation(
1677 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)));
1678 }
1679 }
1680 }
1681}
1682
Geoff Lang7dd2e102014-11-10 15:19:26 -05001683bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1684{
1685 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
1686 {
1687 return false;
1688 }
1689
1690 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1691 {
Jamie Madillf6113162015-05-07 11:49:21 -04001692 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001693 return false;
1694 }
1695
1696 return true;
1697}
1698
1699// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001700bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001701 InfoLog &infoLog,
1702 const AttributeBindings &attributeBindings,
1703 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001705 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001706 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001707 GLuint maxAttribs = data.caps->maxVertexAttributes;
1708
1709 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001710 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001711 {
Jamie Madillf6113162015-05-07 11:49:21 -04001712 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001713 return false;
1714 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001715
Jamie Madillc349ec02015-08-21 16:53:12 -04001716 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001717
Jamie Madillc349ec02015-08-21 16:53:12 -04001718 // Link attributes that have a binding location
1719 for (sh::Attribute &attribute : mData.mAttributes)
1720 {
1721 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001722 ASSERT(attribute.staticUse);
1723
Jamie Madillc349ec02015-08-21 16:53:12 -04001724 int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
1725 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001726 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001727 attribute.location = bindingLocation;
1728 }
1729
1730 if (attribute.location != -1)
1731 {
1732 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001733 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001734
Jamie Madill63805b42015-08-25 13:17:39 -04001735 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001736 {
Jamie Madillf6113162015-05-07 11:49:21 -04001737 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001738 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001739
1740 return false;
1741 }
1742
Jamie Madill63805b42015-08-25 13:17:39 -04001743 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001744 {
Jamie Madill63805b42015-08-25 13:17:39 -04001745 const int regLocation = attribute.location + reg;
1746 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001747
1748 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001749 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001750 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001751 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001752 // TODO(jmadill): fix aliasing on ES2
1753 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001754 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001755 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001756 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001757 return false;
1758 }
1759 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001760 else
1761 {
Jamie Madill63805b42015-08-25 13:17:39 -04001762 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001763 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001764
Jamie Madill63805b42015-08-25 13:17:39 -04001765 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001766 }
1767 }
1768 }
1769
1770 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001771 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001772 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001773 ASSERT(attribute.staticUse);
1774
Jamie Madillc349ec02015-08-21 16:53:12 -04001775 // Not set by glBindAttribLocation or by location layout qualifier
1776 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777 {
Jamie Madill63805b42015-08-25 13:17:39 -04001778 int regs = VariableRegisterCount(attribute.type);
1779 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001780
Jamie Madill63805b42015-08-25 13:17:39 -04001781 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782 {
Jamie Madillf6113162015-05-07 11:49:21 -04001783 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001784 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001785 }
1786
Jamie Madillc349ec02015-08-21 16:53:12 -04001787 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001788 }
1789 }
1790
Jamie Madillc349ec02015-08-21 16:53:12 -04001791 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001793 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001794 ASSERT(attribute.location != -1);
1795 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001796
Jamie Madill63805b42015-08-25 13:17:39 -04001797 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001798 {
Jamie Madill63805b42015-08-25 13:17:39 -04001799 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001800 }
1801 }
1802
Geoff Lang7dd2e102014-11-10 15:19:26 -05001803 return true;
1804}
1805
Jamie Madille473dee2015-08-18 14:49:01 -04001806bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807{
Jamie Madille473dee2015-08-18 14:49:01 -04001808 const Shader &vertexShader = *mData.mAttachedVertexShader;
1809 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1810
Geoff Lang7dd2e102014-11-10 15:19:26 -05001811 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1812 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001813
Geoff Lang7dd2e102014-11-10 15:19:26 -05001814 // Check that interface blocks defined in the vertex and fragment shaders are identical
1815 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1816 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001817
1818 GLuint vertexBlockCount = 0;
1819 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001820 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001822
1823 // Note: shared and std140 layouts are always considered active
1824 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1825 {
1826 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1827 {
1828 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1829 << caps.maxVertexUniformBlocks << ")";
1830 return false;
1831 }
1832 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001833 }
Jamie Madille473dee2015-08-18 14:49:01 -04001834
1835 GLuint fragmentBlockCount = 0;
1836 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837 {
Jamie Madille473dee2015-08-18 14:49:01 -04001838 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001839 if (entry != linkedUniformBlocks.end())
1840 {
1841 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1842 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1843 {
1844 return false;
1845 }
1846 }
Jamie Madille473dee2015-08-18 14:49:01 -04001847
Geoff Lang7dd2e102014-11-10 15:19:26 -05001848 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001849 if (fragmentInterfaceBlock.staticUse ||
1850 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851 {
Jamie Madille473dee2015-08-18 14:49:01 -04001852 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001853 {
Jamie Madille473dee2015-08-18 14:49:01 -04001854 infoLog
1855 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1856 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001857 return false;
1858 }
1859 }
1860 }
Jamie Madille473dee2015-08-18 14:49:01 -04001861
Geoff Lang7dd2e102014-11-10 15:19:26 -05001862 return true;
1863}
1864
1865bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1866 const sh::InterfaceBlock &fragmentInterfaceBlock)
1867{
1868 const char* blockName = vertexInterfaceBlock.name.c_str();
1869 // validate blocks for the same member types
1870 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1871 {
Jamie Madillf6113162015-05-07 11:49:21 -04001872 infoLog << "Types for interface block '" << blockName
1873 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 return false;
1875 }
1876 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1877 {
Jamie Madillf6113162015-05-07 11:49:21 -04001878 infoLog << "Array sizes differ for interface block '" << blockName
1879 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001880 return false;
1881 }
1882 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
1883 {
Jamie Madillf6113162015-05-07 11:49:21 -04001884 infoLog << "Layout qualifiers differ for interface block '" << blockName
1885 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001886 return false;
1887 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001888 const unsigned int numBlockMembers =
1889 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001890 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1891 {
1892 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
1893 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
1894 if (vertexMember.name != fragmentMember.name)
1895 {
Jamie Madillf6113162015-05-07 11:49:21 -04001896 infoLog << "Name mismatch for field " << blockMemberIndex
1897 << " of interface block '" << blockName
1898 << "': (in vertex: '" << vertexMember.name
1899 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900 return false;
1901 }
1902 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
1903 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
1904 {
1905 return false;
1906 }
1907 }
1908 return true;
1909}
1910
1911bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
1912 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
1913{
1914 if (vertexVariable.type != fragmentVariable.type)
1915 {
Jamie Madillf6113162015-05-07 11:49:21 -04001916 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001917 return false;
1918 }
1919 if (vertexVariable.arraySize != fragmentVariable.arraySize)
1920 {
Jamie Madillf6113162015-05-07 11:49:21 -04001921 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001922 return false;
1923 }
1924 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
1925 {
Jamie Madillf6113162015-05-07 11:49:21 -04001926 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001927 return false;
1928 }
1929
1930 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
1931 {
Jamie Madillf6113162015-05-07 11:49:21 -04001932 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001933 return false;
1934 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001935 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001936 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1937 {
1938 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
1939 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
1940
1941 if (vertexMember.name != fragmentMember.name)
1942 {
Jamie Madillf6113162015-05-07 11:49:21 -04001943 infoLog << "Name mismatch for field '" << memberIndex
1944 << "' of " << variableName
1945 << ": (in vertex: '" << vertexMember.name
1946 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001947 return false;
1948 }
1949
1950 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
1951 vertexMember.name + "'";
1952
1953 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
1954 {
1955 return false;
1956 }
1957 }
1958
1959 return true;
1960}
1961
1962bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
1963{
Cooper Partin1acf4382015-06-12 12:38:57 -07001964#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
1965 const bool validatePrecision = true;
1966#else
1967 const bool validatePrecision = false;
1968#endif
1969
1970 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001971 {
1972 return false;
1973 }
1974
1975 return true;
1976}
1977
1978bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
1979{
1980 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
1981 {
1982 return false;
1983 }
1984
Jamie Madille9cc4692015-02-19 16:00:13 -05001985 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001986 {
Jamie Madillf6113162015-05-07 11:49:21 -04001987 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988 return false;
1989 }
1990
1991 return true;
1992}
1993
Jamie Madillccdf74b2015-08-18 10:46:12 -04001994bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
1995 const std::vector<const sh::Varying *> &varyings,
1996 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001997{
1998 size_t totalComponents = 0;
1999
Jamie Madillccdf74b2015-08-18 10:46:12 -04002000 std::set<std::string> uniqueNames;
2001
2002 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003 {
2004 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002005 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002007 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002008 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002009 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002010 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002011 infoLog << "Two transform feedback varyings specify the same output variable ("
2012 << tfVaryingName << ").";
2013 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002015 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002016
Jamie Madillccdf74b2015-08-18 10:46:12 -04002017 // TODO(jmadill): Investigate implementation limits on D3D11
2018 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04002019 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002020 componentCount > caps.maxTransformFeedbackSeparateComponents)
2021 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002022 infoLog << "Transform feedback varying's " << varying->name << " components ("
2023 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002024 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002025 return false;
2026 }
2027
2028 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002029 found = true;
2030 break;
2031 }
2032 }
2033
Jamie Madill89bb70e2015-08-31 14:18:39 -04002034 // TODO(jmadill): investigate if we can support capturing array elements.
2035 if (tfVaryingName.find('[') != std::string::npos)
2036 {
2037 infoLog << "Capture of array elements not currently supported.";
2038 return false;
2039 }
2040
Geoff Lang7dd2e102014-11-10 15:19:26 -05002041 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2042 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002043 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002044 }
2045
Jamie Madillada9ecc2015-08-17 12:53:37 -04002046 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002047 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002048 {
Jamie Madillf6113162015-05-07 11:49:21 -04002049 infoLog << "Transform feedback varying total components (" << totalComponents
2050 << ") exceed the maximum interleaved components ("
2051 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002052 return false;
2053 }
2054
2055 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002056}
2057
Jamie Madillccdf74b2015-08-18 10:46:12 -04002058void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2059{
2060 // Gather the linked varyings that are used for transform feedback, they should all exist.
2061 mData.mTransformFeedbackVaryingVars.clear();
2062 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2063 {
2064 for (const sh::Varying *varying : varyings)
2065 {
2066 if (tfVaryingName == varying->name)
2067 {
2068 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2069 break;
2070 }
2071 }
2072 }
2073}
2074
2075std::vector<const sh::Varying *> Program::getMergedVaryings() const
2076{
2077 std::set<std::string> uniqueNames;
2078 std::vector<const sh::Varying *> varyings;
2079
2080 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2081 {
2082 if (uniqueNames.count(varying.name) == 0)
2083 {
2084 uniqueNames.insert(varying.name);
2085 varyings.push_back(&varying);
2086 }
2087 }
2088
2089 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2090 {
2091 if (uniqueNames.count(varying.name) == 0)
2092 {
2093 uniqueNames.insert(varying.name);
2094 varyings.push_back(&varying);
2095 }
2096 }
2097
2098 return varyings;
2099}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002100
2101void Program::linkOutputVariables()
2102{
2103 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2104 ASSERT(fragmentShader != nullptr);
2105
2106 // Skip this step for GLES2 shaders.
2107 if (fragmentShader->getShaderVersion() == 100)
2108 return;
2109
Jamie Madilla0a9e122015-09-02 15:54:30 -04002110 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002111
2112 // TODO(jmadill): any caps validation here?
2113
2114 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2115 outputVariableIndex++)
2116 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002117 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002118
2119 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2120 if (outputVariable.isBuiltIn())
2121 continue;
2122
2123 // Since multiple output locations must be specified, use 0 for non-specified locations.
2124 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2125
2126 ASSERT(outputVariable.staticUse);
2127
2128 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2129 elementIndex++)
2130 {
2131 const int location = baseLocation + elementIndex;
2132 ASSERT(mData.mOutputVariables.count(location) == 0);
2133 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2134 mData.mOutputVariables[location] =
2135 VariableLocation(outputVariable.name, element, outputVariableIndex);
2136 }
2137 }
2138}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002139
2140bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2141{
2142 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2143 VectorAndSamplerCount vsCounts;
2144
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002145 std::vector<LinkedUniform> samplerUniforms;
2146
Jamie Madill62d31cb2015-09-11 13:25:51 -04002147 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2148 {
2149 if (uniform.staticUse)
2150 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002151 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002152 }
2153 }
2154
2155 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2156 {
2157 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2158 << caps.maxVertexUniformVectors << ").";
2159 return false;
2160 }
2161
2162 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2163 {
2164 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2165 << caps.maxVertexTextureImageUnits << ").";
2166 return false;
2167 }
2168
2169 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2170 VectorAndSamplerCount fsCounts;
2171
2172 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2173 {
2174 if (uniform.staticUse)
2175 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002176 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002177 }
2178 }
2179
2180 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2181 {
2182 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2183 << caps.maxFragmentUniformVectors << ").";
2184 return false;
2185 }
2186
2187 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2188 {
2189 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2190 << caps.maxTextureImageUnits << ").";
2191 return false;
2192 }
2193
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002194 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2195 mSamplerUniformRange.end =
2196 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2197
2198 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2199
Jamie Madill62d31cb2015-09-11 13:25:51 -04002200 return true;
2201}
2202
2203Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002204 const std::string &fullName,
2205 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002206{
2207 VectorAndSamplerCount vectorAndSamplerCount;
2208
2209 if (uniform.isStruct())
2210 {
2211 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2212 {
2213 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2214
2215 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2216 {
2217 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2218 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2219
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002220 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002221 }
2222 }
2223
2224 return vectorAndSamplerCount;
2225 }
2226
2227 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002228 bool isSampler = IsSamplerType(uniform.type);
2229 if (!UniformInList(mData.getUniforms(), fullName) && !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002230 {
2231 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2232 uniform.arraySize, -1,
2233 sh::BlockMemberInfo::getDefaultBlockInfo());
2234 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002235
2236 // Store sampler uniforms separately, so we'll append them to the end of the list.
2237 if (isSampler)
2238 {
2239 samplerUniforms->push_back(linkedUniform);
2240 }
2241 else
2242 {
2243 mData.mUniforms.push_back(linkedUniform);
2244 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002245 }
2246
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002247 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002248
2249 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2250 // Likewise, don't count "real" uniforms towards sampler count.
2251 vectorAndSamplerCount.vectorCount =
2252 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002253 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002254
2255 return vectorAndSamplerCount;
2256}
2257
2258void Program::gatherInterfaceBlockInfo()
2259{
2260 std::set<std::string> visitedList;
2261
2262 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2263
2264 ASSERT(mData.mUniformBlocks.empty());
2265 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2266 {
2267 // Only 'packed' blocks are allowed to be considered inacive.
2268 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2269 continue;
2270
2271 if (visitedList.count(vertexBlock.name) > 0)
2272 continue;
2273
2274 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2275 visitedList.insert(vertexBlock.name);
2276 }
2277
2278 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2279
2280 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2281 {
2282 // Only 'packed' blocks are allowed to be considered inacive.
2283 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2284 continue;
2285
2286 if (visitedList.count(fragmentBlock.name) > 0)
2287 {
2288 for (gl::UniformBlock &block : mData.mUniformBlocks)
2289 {
2290 if (block.name == fragmentBlock.name)
2291 {
2292 block.fragmentStaticUse = fragmentBlock.staticUse;
2293 }
2294 }
2295
2296 continue;
2297 }
2298
2299 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2300 visitedList.insert(fragmentBlock.name);
2301 }
2302}
2303
Jamie Madill4a3c2342015-10-08 12:58:45 -04002304template <typename VarT>
2305void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2306 const std::string &prefix,
2307 int blockIndex)
2308{
2309 for (const VarT &field : fields)
2310 {
2311 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2312
2313 if (field.isStruct())
2314 {
2315 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2316 {
2317 const std::string uniformElementName =
2318 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2319 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2320 }
2321 }
2322 else
2323 {
2324 // If getBlockMemberInfo returns false, the uniform is optimized out.
2325 sh::BlockMemberInfo memberInfo;
2326 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2327 {
2328 continue;
2329 }
2330
2331 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2332 blockIndex, memberInfo);
2333
2334 // Since block uniforms have no location, we don't need to store them in the uniform
2335 // locations list.
2336 mData.mUniforms.push_back(newUniform);
2337 }
2338 }
2339}
2340
Jamie Madill62d31cb2015-09-11 13:25:51 -04002341void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2342{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002343 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2344 size_t blockSize = 0;
2345
2346 // Don't define this block at all if it's not active in the implementation.
2347 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2348 {
2349 return;
2350 }
2351
2352 // Track the first and last uniform index to determine the range of active uniforms in the
2353 // block.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002354 size_t firstBlockUniformIndex = mData.mUniforms.size();
Jamie Madill4a3c2342015-10-08 12:58:45 -04002355 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002356 size_t lastBlockUniformIndex = mData.mUniforms.size();
2357
2358 std::vector<unsigned int> blockUniformIndexes;
2359 for (size_t blockUniformIndex = firstBlockUniformIndex;
2360 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2361 {
2362 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2363 }
2364
2365 if (interfaceBlock.arraySize > 0)
2366 {
2367 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2368 {
2369 UniformBlock block(interfaceBlock.name, true, arrayElement);
2370 block.memberUniformIndexes = blockUniformIndexes;
2371
2372 if (shaderType == GL_VERTEX_SHADER)
2373 {
2374 block.vertexStaticUse = interfaceBlock.staticUse;
2375 }
2376 else
2377 {
2378 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2379 block.fragmentStaticUse = interfaceBlock.staticUse;
2380 }
2381
Jamie Madill4a3c2342015-10-08 12:58:45 -04002382 // TODO(jmadill): Determine if we can ever have an inactive array element block.
2383 size_t blockElementSize = 0;
2384 if (!mProgram->getUniformBlockSize(block.nameWithArrayIndex(), &blockElementSize))
2385 {
2386 continue;
2387 }
2388
2389 ASSERT(blockElementSize == blockSize);
2390 block.dataSize = static_cast<unsigned int>(blockElementSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002391 mData.mUniformBlocks.push_back(block);
2392 }
2393 }
2394 else
2395 {
2396 UniformBlock block(interfaceBlock.name, false, 0);
2397 block.memberUniformIndexes = blockUniformIndexes;
2398
2399 if (shaderType == GL_VERTEX_SHADER)
2400 {
2401 block.vertexStaticUse = interfaceBlock.staticUse;
2402 }
2403 else
2404 {
2405 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2406 block.fragmentStaticUse = interfaceBlock.staticUse;
2407 }
2408
Jamie Madill4a3c2342015-10-08 12:58:45 -04002409 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002410 mData.mUniformBlocks.push_back(block);
2411 }
2412}
2413
2414template <typename T>
2415void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2416{
2417 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2418 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2419 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2420
2421 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2422 {
2423 // Do a cast conversion for boolean types. From the spec:
2424 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2425 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2426 for (GLsizei component = 0; component < count; ++component)
2427 {
2428 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2429 }
2430 }
2431 else
2432 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002433 // Invalide the validation cache if we modify the sampler data.
2434 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2435 {
2436 mCachedValidateSamplersResult.reset();
2437 }
2438
Jamie Madill62d31cb2015-09-11 13:25:51 -04002439 memcpy(destPointer, v, sizeof(T) * count);
2440 }
2441}
2442
2443template <size_t cols, size_t rows, typename T>
2444void Program::setMatrixUniformInternal(GLint location,
2445 GLsizei count,
2446 GLboolean transpose,
2447 const T *v)
2448{
2449 if (!transpose)
2450 {
2451 setUniformInternal(location, count * cols * rows, v);
2452 return;
2453 }
2454
2455 // Perform a transposing copy.
2456 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2457 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2458 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2459 for (GLsizei element = 0; element < count; ++element)
2460 {
2461 size_t elementOffset = element * rows * cols;
2462
2463 for (size_t row = 0; row < rows; ++row)
2464 {
2465 for (size_t col = 0; col < cols; ++col)
2466 {
2467 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2468 }
2469 }
2470 }
2471}
2472
2473template <typename DestT>
2474void Program::getUniformInternal(GLint location, DestT *dataOut) const
2475{
2476 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2477 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2478
2479 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2480
2481 GLenum componentType = VariableComponentType(uniform.type);
2482 if (componentType == GLTypeToGLenum<DestT>::value)
2483 {
2484 memcpy(dataOut, srcPointer, uniform.getElementSize());
2485 return;
2486 }
2487
2488 int components = VariableComponentCount(uniform.type) * uniform.elementCount();
2489
2490 switch (componentType)
2491 {
2492 case GL_INT:
2493 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2494 break;
2495 case GL_UNSIGNED_INT:
2496 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2497 break;
2498 case GL_BOOL:
2499 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2500 break;
2501 case GL_FLOAT:
2502 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2503 break;
2504 default:
2505 UNREACHABLE();
2506 }
2507}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508}