blob: c24e80216973d3c02e7a069a792d0dc2e5404ca9 [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
Jamie Madillada9ecc2015-08-17 12:53:37 -0400627 stream.readInt(&mData.mTransformFeedbackBufferMode);
628
Jamie Madill80a6fc02015-08-21 16:53:16 -0400629 unsigned int outputVarCount = stream.readInt<unsigned int>();
630 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
631 {
632 int locationIndex = stream.readInt<int>();
633 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400634 stream.readInt(&locationData.element);
635 stream.readInt(&locationData.index);
636 stream.readString(&locationData.name);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400637 mData.mOutputVariables[locationIndex] = locationData;
638 }
639
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400640 stream.readInt(&mSamplerUniformRange.start);
641 stream.readInt(&mSamplerUniformRange.end);
642
Geoff Lang7dd2e102014-11-10 15:19:26 -0500643 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
644 if (result.error.isError() || !result.linkSuccess)
645 {
Geoff Langb543aff2014-09-30 14:52:54 -0400646 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000647 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000648
Geoff Lang7dd2e102014-11-10 15:19:26 -0500649 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400650 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500651#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
652}
653
654Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
655{
656 if (binaryFormat)
657 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400658 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500659 }
660
661 BinaryOutputStream stream;
662
Geoff Lang7dd2e102014-11-10 15:19:26 -0500663 stream.writeInt(ANGLE_MAJOR_VERSION);
664 stream.writeInt(ANGLE_MINOR_VERSION);
665 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
666
Jamie Madill63805b42015-08-25 13:17:39 -0400667 stream.writeInt(mData.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500668
Jamie Madillc349ec02015-08-21 16:53:12 -0400669 stream.writeInt(mData.mAttributes.size());
670 for (const sh::Attribute &attrib : mData.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400671 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400672 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400673 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400674 }
675
676 stream.writeInt(mData.mUniforms.size());
677 for (const gl::LinkedUniform &uniform : mData.mUniforms)
678 {
679 WriteShaderVar(&stream, uniform);
680
681 // FIXME: referenced
682
683 stream.writeInt(uniform.blockIndex);
684 stream.writeInt(uniform.blockInfo.offset);
685 stream.writeInt(uniform.blockInfo.arrayStride);
686 stream.writeInt(uniform.blockInfo.matrixStride);
687 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
688 }
689
690 stream.writeInt(mData.mUniformLocations.size());
691 for (const auto &variable : mData.mUniformLocations)
692 {
693 stream.writeString(variable.name);
694 stream.writeInt(variable.element);
695 stream.writeInt(variable.index);
696 }
697
698 stream.writeInt(mData.mUniformBlocks.size());
699 for (const UniformBlock &uniformBlock : mData.mUniformBlocks)
700 {
701 stream.writeString(uniformBlock.name);
702 stream.writeInt(uniformBlock.isArray);
703 stream.writeInt(uniformBlock.arrayElement);
704 stream.writeInt(uniformBlock.dataSize);
705
706 stream.writeInt(uniformBlock.vertexStaticUse);
707 stream.writeInt(uniformBlock.fragmentStaticUse);
708
709 stream.writeInt(uniformBlock.memberUniformIndexes.size());
710 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
711 {
712 stream.writeInt(memberUniformIndex);
713 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400714 }
715
Jamie Madillada9ecc2015-08-17 12:53:37 -0400716 stream.writeInt(mData.mTransformFeedbackBufferMode);
717
Jamie Madill80a6fc02015-08-21 16:53:16 -0400718 stream.writeInt(mData.mOutputVariables.size());
719 for (const auto &outputPair : mData.mOutputVariables)
720 {
721 stream.writeInt(outputPair.first);
722 stream.writeInt(outputPair.second.element);
723 stream.writeInt(outputPair.second.index);
724 stream.writeString(outputPair.second.name);
725 }
726
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400727 stream.writeInt(mSamplerUniformRange.start);
728 stream.writeInt(mSamplerUniformRange.end);
729
Geoff Lang7dd2e102014-11-10 15:19:26 -0500730 gl::Error error = mProgram->save(&stream);
731 if (error.isError())
732 {
733 return error;
734 }
735
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700736 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500737 const void *streamData = stream.data();
738
739 if (streamLength > bufSize)
740 {
741 if (length)
742 {
743 *length = 0;
744 }
745
746 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
747 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
748 // sizes and then copy it.
749 return Error(GL_INVALID_OPERATION);
750 }
751
752 if (binary)
753 {
754 char *ptr = reinterpret_cast<char*>(binary);
755
756 memcpy(ptr, streamData, streamLength);
757 ptr += streamLength;
758
759 ASSERT(ptr - streamLength == binary);
760 }
761
762 if (length)
763 {
764 *length = streamLength;
765 }
766
767 return Error(GL_NO_ERROR);
768}
769
770GLint Program::getBinaryLength() const
771{
772 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400773 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500774 if (error.isError())
775 {
776 return 0;
777 }
778
779 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000780}
781
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000782void Program::release()
783{
784 mRefCount--;
785
786 if (mRefCount == 0 && mDeleteStatus)
787 {
788 mResourceManager->deleteProgram(mHandle);
789 }
790}
791
792void Program::addRef()
793{
794 mRefCount++;
795}
796
797unsigned int Program::getRefCount() const
798{
799 return mRefCount;
800}
801
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000802int Program::getInfoLogLength() const
803{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400804 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000805}
806
Geoff Lange1a27752015-10-05 13:16:04 -0400807void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000808{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000809 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000810}
811
Geoff Lange1a27752015-10-05 13:16:04 -0400812void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000813{
814 int total = 0;
815
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400816 if (mData.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000817 {
818 if (total < maxCount)
819 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400820 shaders[total] = mData.mAttachedVertexShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000821 }
822
823 total++;
824 }
825
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400826 if (mData.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000827 {
828 if (total < maxCount)
829 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400830 shaders[total] = mData.mAttachedFragmentShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000831 }
832
833 total++;
834 }
835
836 if (count)
837 {
838 *count = total;
839 }
840}
841
Geoff Lange1a27752015-10-05 13:16:04 -0400842GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500843{
Jamie Madillc349ec02015-08-21 16:53:12 -0400844 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500845 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400846 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500847 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400848 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500849 }
850 }
851
Austin Kinrossb8af7232015-03-16 22:33:25 -0700852 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500853}
854
Jamie Madill63805b42015-08-25 13:17:39 -0400855bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400856{
Olli Etuaho401d9fe2015-08-26 10:19:59 +0300857 ASSERT(attribLocation < mData.mActiveAttribLocationsMask.size());
Jamie Madill63805b42015-08-25 13:17:39 -0400858 return mData.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500859}
860
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000861void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
862{
Jamie Madillc349ec02015-08-21 16:53:12 -0400863 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000864 {
865 if (bufsize > 0)
866 {
867 name[0] = '\0';
868 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500869
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000870 if (length)
871 {
872 *length = 0;
873 }
874
875 *type = GL_NONE;
876 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400877 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000878 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400879
880 size_t attributeIndex = 0;
881
882 for (const sh::Attribute &attribute : mData.mAttributes)
883 {
884 // Skip over inactive attributes
885 if (attribute.staticUse)
886 {
887 if (static_cast<size_t>(index) == attributeIndex)
888 {
889 break;
890 }
891 attributeIndex++;
892 }
893 }
894
895 ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
896 const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
897
898 if (bufsize > 0)
899 {
900 const char *string = attrib.name.c_str();
901
902 strncpy(name, string, bufsize);
903 name[bufsize - 1] = '\0';
904
905 if (length)
906 {
907 *length = static_cast<GLsizei>(strlen(name));
908 }
909 }
910
911 // Always a single 'type' instance
912 *size = 1;
913 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000914}
915
Geoff Lange1a27752015-10-05 13:16:04 -0400916GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000917{
Jamie Madillc349ec02015-08-21 16:53:12 -0400918 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400919 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400920 return 0;
921 }
922
923 GLint count = 0;
924
925 for (const sh::Attribute &attrib : mData.mAttributes)
926 {
927 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000928 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500929
930 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000931}
932
Geoff Lange1a27752015-10-05 13:16:04 -0400933GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000934{
Jamie Madillc349ec02015-08-21 16:53:12 -0400935 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400936 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400937 return 0;
938 }
939
940 size_t maxLength = 0;
941
942 for (const sh::Attribute &attrib : mData.mAttributes)
943 {
944 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500945 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400946 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500947 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000948 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500949
Jamie Madillc349ec02015-08-21 16:53:12 -0400950 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500951}
952
Geoff Lang7dd2e102014-11-10 15:19:26 -0500953GLint Program::getFragDataLocation(const std::string &name) const
954{
955 std::string baseName(name);
956 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400957 for (auto outputPair : mData.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000958 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400959 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500960 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
961 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400962 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500963 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000964 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500965 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000966}
967
Geoff Lange1a27752015-10-05 13:16:04 -0400968void Program::getActiveUniform(GLuint index,
969 GLsizei bufsize,
970 GLsizei *length,
971 GLint *size,
972 GLenum *type,
973 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000974{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500975 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000976 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400977 // index must be smaller than getActiveUniformCount()
978 ASSERT(index < mData.mUniforms.size());
979 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500980
981 if (bufsize > 0)
982 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400983 std::string string = uniform.name;
984 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500985 {
986 string += "[0]";
987 }
988
989 strncpy(name, string.c_str(), bufsize);
990 name[bufsize - 1] = '\0';
991
992 if (length)
993 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700994 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -0500995 }
996 }
997
Jamie Madill62d31cb2015-09-11 13:25:51 -0400998 *size = uniform.elementCount();
999 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001000 }
1001 else
1002 {
1003 if (bufsize > 0)
1004 {
1005 name[0] = '\0';
1006 }
1007
1008 if (length)
1009 {
1010 *length = 0;
1011 }
1012
1013 *size = 0;
1014 *type = GL_NONE;
1015 }
1016}
1017
Geoff Lange1a27752015-10-05 13:16:04 -04001018GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001019{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001020 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001021 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001022 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001023 }
1024 else
1025 {
1026 return 0;
1027 }
1028}
1029
Geoff Lange1a27752015-10-05 13:16:04 -04001030GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001031{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001032 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001033
1034 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001035 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001036 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001037 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001038 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001039 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001040 size_t length = uniform.name.length() + 1u;
1041 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001042 {
1043 length += 3; // Counting in "[0]".
1044 }
1045 maxLength = std::max(length, maxLength);
1046 }
1047 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001048 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001049
Jamie Madill62d31cb2015-09-11 13:25:51 -04001050 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001051}
1052
1053GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1054{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001055 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1056 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001057 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001058 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001059 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1060 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1061 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1062 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1063 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1064 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1065 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1066 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1067 default:
1068 UNREACHABLE();
1069 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001070 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001071 return 0;
1072}
1073
1074bool Program::isValidUniformLocation(GLint location) const
1075{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001076 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
1077 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001078}
1079
Jamie Madill62d31cb2015-09-11 13:25:51 -04001080const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001082 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1083 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001084}
1085
Jamie Madill62d31cb2015-09-11 13:25:51 -04001086GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001087{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001088 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001089}
1090
Jamie Madill62d31cb2015-09-11 13:25:51 -04001091GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001092{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001093 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001094}
1095
1096void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1097{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001098 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099 mProgram->setUniform1fv(location, count, v);
1100}
1101
1102void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1103{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001104 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001105 mProgram->setUniform2fv(location, count, v);
1106}
1107
1108void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1109{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001110 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001111 mProgram->setUniform3fv(location, count, v);
1112}
1113
1114void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1115{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001116 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001117 mProgram->setUniform4fv(location, count, v);
1118}
1119
1120void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1121{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001122 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001123 mProgram->setUniform1iv(location, count, v);
1124}
1125
1126void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1127{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001128 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001129 mProgram->setUniform2iv(location, count, v);
1130}
1131
1132void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1133{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001134 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001135 mProgram->setUniform3iv(location, count, v);
1136}
1137
1138void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1139{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001140 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001141 mProgram->setUniform4iv(location, count, v);
1142}
1143
1144void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1145{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001146 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147 mProgram->setUniform1uiv(location, count, v);
1148}
1149
1150void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1151{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001152 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001153 mProgram->setUniform2uiv(location, count, v);
1154}
1155
1156void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1157{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001158 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001159 mProgram->setUniform3uiv(location, count, v);
1160}
1161
1162void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1163{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001164 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001165 mProgram->setUniform4uiv(location, count, v);
1166}
1167
1168void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1169{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001170 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001171 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1172}
1173
1174void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1175{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001176 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001177 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1178}
1179
1180void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1181{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001182 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001183 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1184}
1185
1186void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1187{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001188 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001189 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1190}
1191
1192void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1193{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001194 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001195 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1196}
1197
1198void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1199{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001200 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001201 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1202}
1203
1204void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1205{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001206 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001207 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1208}
1209
1210void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1211{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001212 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001213 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1214}
1215
1216void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1217{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001218 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001219 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1220}
1221
Geoff Lange1a27752015-10-05 13:16:04 -04001222void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001224 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001225}
1226
Geoff Lange1a27752015-10-05 13:16:04 -04001227void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001228{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001229 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001230}
1231
Geoff Lange1a27752015-10-05 13:16:04 -04001232void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001233{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001234 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001235}
1236
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001237void Program::flagForDeletion()
1238{
1239 mDeleteStatus = true;
1240}
1241
1242bool Program::isFlaggedForDeletion() const
1243{
1244 return mDeleteStatus;
1245}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001246
Brandon Jones43a53e22014-08-28 16:23:22 -07001247void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001248{
1249 mInfoLog.reset();
1250
Geoff Lang7dd2e102014-11-10 15:19:26 -05001251 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001252 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001253 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001254 }
1255 else
1256 {
Jamie Madillf6113162015-05-07 11:49:21 -04001257 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001258 }
1259}
1260
Geoff Lang7dd2e102014-11-10 15:19:26 -05001261bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1262{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001263 // Skip cache if we're using an infolog, so we get the full error.
1264 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1265 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1266 {
1267 return mCachedValidateSamplersResult.value();
1268 }
1269
1270 if (mTextureUnitTypesCache.empty())
1271 {
1272 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1273 }
1274 else
1275 {
1276 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1277 }
1278
1279 // if any two active samplers in a program are of different types, but refer to the same
1280 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1281 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1282 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1283 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1284 {
1285 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1286 ASSERT(uniform.isSampler());
1287
1288 if (!uniform.staticUse)
1289 continue;
1290
1291 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1292 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1293
1294 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1295 {
1296 GLuint textureUnit = dataPtr[arrayElement];
1297
1298 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1299 {
1300 if (infoLog)
1301 {
1302 (*infoLog) << "Sampler uniform (" << textureUnit
1303 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1304 << caps.maxCombinedTextureImageUnits << ")";
1305 }
1306
1307 mCachedValidateSamplersResult = false;
1308 return false;
1309 }
1310
1311 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1312 {
1313 if (textureType != mTextureUnitTypesCache[textureUnit])
1314 {
1315 if (infoLog)
1316 {
1317 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1318 "image unit ("
1319 << textureUnit << ").";
1320 }
1321
1322 mCachedValidateSamplersResult = false;
1323 return false;
1324 }
1325 }
1326 else
1327 {
1328 mTextureUnitTypesCache[textureUnit] = textureType;
1329 }
1330 }
1331 }
1332
1333 mCachedValidateSamplersResult = true;
1334 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001335}
1336
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001337bool Program::isValidated() const
1338{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001339 return mValidated;
1340}
1341
Geoff Lange1a27752015-10-05 13:16:04 -04001342GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001344 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001345}
1346
1347void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1348{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001349 ASSERT(uniformBlockIndex <
1350 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001351
Jamie Madill62d31cb2015-09-11 13:25:51 -04001352 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353
1354 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001355 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001356 std::string string = uniformBlock.name;
1357
Jamie Madill62d31cb2015-09-11 13:25:51 -04001358 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001360 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001361 }
1362
1363 strncpy(uniformBlockName, string.c_str(), bufSize);
1364 uniformBlockName[bufSize - 1] = '\0';
1365
1366 if (length)
1367 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001368 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001369 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001370 }
1371}
1372
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001374{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001375 ASSERT(uniformBlockIndex <
1376 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377
Jamie Madill62d31cb2015-09-11 13:25:51 -04001378 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379
1380 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001381 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382 case GL_UNIFORM_BLOCK_DATA_SIZE:
1383 *params = static_cast<GLint>(uniformBlock.dataSize);
1384 break;
1385 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001386 *params =
1387 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001388 break;
1389 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1390 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1391 break;
1392 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1393 {
1394 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1395 {
1396 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1397 }
1398 }
1399 break;
1400 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001401 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402 break;
1403 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001404 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405 break;
1406 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001407 }
1408}
1409
Geoff Lange1a27752015-10-05 13:16:04 -04001410GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001411{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001412 int maxLength = 0;
1413
1414 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001415 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001416 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001417 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1418 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001419 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001420 if (!uniformBlock.name.empty())
1421 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001422 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001423
1424 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001425 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426
1427 maxLength = std::max(length + arrayLength, maxLength);
1428 }
1429 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001430 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001431
1432 return maxLength;
1433}
1434
Geoff Lange1a27752015-10-05 13:16:04 -04001435GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001436{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001437 size_t subscript = GL_INVALID_INDEX;
1438 std::string baseName = gl::ParseUniformName(name, &subscript);
1439
1440 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1441 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1442 {
1443 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1444 if (uniformBlock.name == baseName)
1445 {
1446 const bool arrayElementZero =
1447 (subscript == GL_INVALID_INDEX &&
1448 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1449 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1450 {
1451 return blockIndex;
1452 }
1453 }
1454 }
1455
1456 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001457}
1458
Jamie Madill62d31cb2015-09-11 13:25:51 -04001459const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001460{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001461 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1462 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001463}
1464
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001465void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1466{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001467 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001468 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001469}
1470
1471GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1472{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001473 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001474}
1475
1476void Program::resetUniformBlockBindings()
1477{
1478 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1479 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001480 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001481 }
Geoff Lang5d124a62015-09-15 13:03:27 -04001482 mData.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001483}
1484
Geoff Lang48dcae72014-02-05 16:28:24 -05001485void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1486{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001487 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001488 for (GLsizei i = 0; i < count; i++)
1489 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001490 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001491 }
1492
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001493 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001494}
1495
1496void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1497{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001498 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001499 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001500 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1501 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001502 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1503 if (length)
1504 {
1505 *length = lastNameIdx;
1506 }
1507 if (size)
1508 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001509 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001510 }
1511 if (type)
1512 {
1513 *type = varying.type;
1514 }
1515 if (name)
1516 {
1517 memcpy(name, varying.name.c_str(), lastNameIdx);
1518 name[lastNameIdx] = '\0';
1519 }
1520 }
1521}
1522
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001523GLsizei Program::getTransformFeedbackVaryingCount() const
1524{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001526 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001527 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001528 }
1529 else
1530 {
1531 return 0;
1532 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001533}
1534
1535GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1536{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001537 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001538 {
1539 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001540 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001541 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001542 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1543 }
1544
1545 return maxSize;
1546 }
1547 else
1548 {
1549 return 0;
1550 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001551}
1552
1553GLenum Program::getTransformFeedbackBufferMode() const
1554{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001555 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001556}
1557
Jamie Madillada9ecc2015-08-17 12:53:37 -04001558// static
1559bool Program::linkVaryings(InfoLog &infoLog,
1560 const Shader *vertexShader,
1561 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001562{
Jamie Madill4cff2472015-08-21 16:53:18 -04001563 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1564 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001565
Jamie Madill4cff2472015-08-21 16:53:18 -04001566 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001568 bool matched = false;
1569
1570 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001571 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001572 {
1573 continue;
1574 }
1575
Jamie Madill4cff2472015-08-21 16:53:18 -04001576 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001577 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001578 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001579 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001580 ASSERT(!input.isBuiltIn());
1581 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001582 {
1583 return false;
1584 }
1585
Geoff Lang7dd2e102014-11-10 15:19:26 -05001586 matched = true;
1587 break;
1588 }
1589 }
1590
1591 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001592 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001593 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001594 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001595 return false;
1596 }
1597 }
1598
Jamie Madillada9ecc2015-08-17 12:53:37 -04001599 // TODO(jmadill): verify no unmatched vertex varyings?
1600
Geoff Lang7dd2e102014-11-10 15:19:26 -05001601 return true;
1602}
1603
Jamie Madill62d31cb2015-09-11 13:25:51 -04001604bool Program::linkUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Jamie Madillea918db2015-08-18 14:48:59 -04001605{
1606 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1607 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1608
1609 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001610 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001611
1612 for (const sh::Uniform &vertexUniform : vertexUniforms)
1613 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001614 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001615 }
1616
1617 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1618 {
1619 auto entry = linkedUniforms.find(fragmentUniform.name);
1620 if (entry != linkedUniforms.end())
1621 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001622 LinkedUniform *vertexUniform = &entry->second;
1623 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1624 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001625 {
1626 return false;
1627 }
1628 }
1629 }
1630
Jamie Madill62d31cb2015-09-11 13:25:51 -04001631 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1632 // Also check the maximum uniform vector and sampler counts.
1633 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1634 {
1635 return false;
1636 }
1637
1638 indexUniforms();
1639
Jamie Madillea918db2015-08-18 14:48:59 -04001640 return true;
1641}
1642
Jamie Madill62d31cb2015-09-11 13:25:51 -04001643void Program::indexUniforms()
1644{
1645 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1646 {
1647 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1648
1649 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1650 {
1651 if (!uniform.isBuiltIn())
1652 {
1653 // Assign in-order uniform locations
1654 mData.mUniformLocations.push_back(gl::VariableLocation(
1655 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)));
1656 }
1657 }
1658 }
1659}
1660
Geoff Lang7dd2e102014-11-10 15:19:26 -05001661bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1662{
1663 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
1664 {
1665 return false;
1666 }
1667
1668 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1669 {
Jamie Madillf6113162015-05-07 11:49:21 -04001670 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671 return false;
1672 }
1673
1674 return true;
1675}
1676
1677// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001678bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001679 InfoLog &infoLog,
1680 const AttributeBindings &attributeBindings,
1681 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001683 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001684 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001685 GLuint maxAttribs = data.caps->maxVertexAttributes;
1686
1687 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001688 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001689 {
Jamie Madillf6113162015-05-07 11:49:21 -04001690 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001691 return false;
1692 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001693
Jamie Madillc349ec02015-08-21 16:53:12 -04001694 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001695
Jamie Madillc349ec02015-08-21 16:53:12 -04001696 // Link attributes that have a binding location
1697 for (sh::Attribute &attribute : mData.mAttributes)
1698 {
1699 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700 ASSERT(attribute.staticUse);
1701
Jamie Madillc349ec02015-08-21 16:53:12 -04001702 int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
1703 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001704 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001705 attribute.location = bindingLocation;
1706 }
1707
1708 if (attribute.location != -1)
1709 {
1710 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001711 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001712
Jamie Madill63805b42015-08-25 13:17:39 -04001713 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001714 {
Jamie Madillf6113162015-05-07 11:49:21 -04001715 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001716 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001717
1718 return false;
1719 }
1720
Jamie Madill63805b42015-08-25 13:17:39 -04001721 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001722 {
Jamie Madill63805b42015-08-25 13:17:39 -04001723 const int regLocation = attribute.location + reg;
1724 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001725
1726 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001727 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001728 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001729 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001730 // TODO(jmadill): fix aliasing on ES2
1731 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001732 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001733 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001734 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001735 return false;
1736 }
1737 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001738 else
1739 {
Jamie Madill63805b42015-08-25 13:17:39 -04001740 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001741 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001742
Jamie Madill63805b42015-08-25 13:17:39 -04001743 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001744 }
1745 }
1746 }
1747
1748 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001749 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001750 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001751 ASSERT(attribute.staticUse);
1752
Jamie Madillc349ec02015-08-21 16:53:12 -04001753 // Not set by glBindAttribLocation or by location layout qualifier
1754 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001755 {
Jamie Madill63805b42015-08-25 13:17:39 -04001756 int regs = VariableRegisterCount(attribute.type);
1757 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001758
Jamie Madill63805b42015-08-25 13:17:39 -04001759 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001760 {
Jamie Madillf6113162015-05-07 11:49:21 -04001761 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001762 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001763 }
1764
Jamie Madillc349ec02015-08-21 16:53:12 -04001765 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001766 }
1767 }
1768
Jamie Madillc349ec02015-08-21 16:53:12 -04001769 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001770 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001771 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001772 ASSERT(attribute.location != -1);
1773 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001774
Jamie Madill63805b42015-08-25 13:17:39 -04001775 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001776 {
Jamie Madill63805b42015-08-25 13:17:39 -04001777 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001778 }
1779 }
1780
Geoff Lang7dd2e102014-11-10 15:19:26 -05001781 return true;
1782}
1783
Jamie Madille473dee2015-08-18 14:49:01 -04001784bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001785{
Jamie Madille473dee2015-08-18 14:49:01 -04001786 const Shader &vertexShader = *mData.mAttachedVertexShader;
1787 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1788
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1790 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001791
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792 // Check that interface blocks defined in the vertex and fragment shaders are identical
1793 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1794 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001795
1796 GLuint vertexBlockCount = 0;
1797 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001798 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001799 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001800
1801 // Note: shared and std140 layouts are always considered active
1802 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1803 {
1804 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1805 {
1806 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1807 << caps.maxVertexUniformBlocks << ")";
1808 return false;
1809 }
1810 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001811 }
Jamie Madille473dee2015-08-18 14:49:01 -04001812
1813 GLuint fragmentBlockCount = 0;
1814 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001815 {
Jamie Madille473dee2015-08-18 14:49:01 -04001816 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001817 if (entry != linkedUniformBlocks.end())
1818 {
1819 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1820 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1821 {
1822 return false;
1823 }
1824 }
Jamie Madille473dee2015-08-18 14:49:01 -04001825
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001827 if (fragmentInterfaceBlock.staticUse ||
1828 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829 {
Jamie Madille473dee2015-08-18 14:49:01 -04001830 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001831 {
Jamie Madille473dee2015-08-18 14:49:01 -04001832 infoLog
1833 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1834 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001835 return false;
1836 }
1837 }
1838 }
Jamie Madille473dee2015-08-18 14:49:01 -04001839
Geoff Lang7dd2e102014-11-10 15:19:26 -05001840 return true;
1841}
1842
1843bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1844 const sh::InterfaceBlock &fragmentInterfaceBlock)
1845{
1846 const char* blockName = vertexInterfaceBlock.name.c_str();
1847 // validate blocks for the same member types
1848 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1849 {
Jamie Madillf6113162015-05-07 11:49:21 -04001850 infoLog << "Types for interface block '" << blockName
1851 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001852 return false;
1853 }
1854 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1855 {
Jamie Madillf6113162015-05-07 11:49:21 -04001856 infoLog << "Array sizes differ for interface block '" << blockName
1857 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001858 return false;
1859 }
1860 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
1861 {
Jamie Madillf6113162015-05-07 11:49:21 -04001862 infoLog << "Layout qualifiers differ for interface block '" << blockName
1863 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001864 return false;
1865 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001866 const unsigned int numBlockMembers =
1867 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1869 {
1870 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
1871 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
1872 if (vertexMember.name != fragmentMember.name)
1873 {
Jamie Madillf6113162015-05-07 11:49:21 -04001874 infoLog << "Name mismatch for field " << blockMemberIndex
1875 << " of interface block '" << blockName
1876 << "': (in vertex: '" << vertexMember.name
1877 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001878 return false;
1879 }
1880 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
1881 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
1882 {
1883 return false;
1884 }
1885 }
1886 return true;
1887}
1888
1889bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
1890 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
1891{
1892 if (vertexVariable.type != fragmentVariable.type)
1893 {
Jamie Madillf6113162015-05-07 11:49:21 -04001894 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001895 return false;
1896 }
1897 if (vertexVariable.arraySize != fragmentVariable.arraySize)
1898 {
Jamie Madillf6113162015-05-07 11:49:21 -04001899 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900 return false;
1901 }
1902 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
1903 {
Jamie Madillf6113162015-05-07 11:49:21 -04001904 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001905 return false;
1906 }
1907
1908 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
1909 {
Jamie Madillf6113162015-05-07 11:49:21 -04001910 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001911 return false;
1912 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001913 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001914 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1915 {
1916 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
1917 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
1918
1919 if (vertexMember.name != fragmentMember.name)
1920 {
Jamie Madillf6113162015-05-07 11:49:21 -04001921 infoLog << "Name mismatch for field '" << memberIndex
1922 << "' of " << variableName
1923 << ": (in vertex: '" << vertexMember.name
1924 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001925 return false;
1926 }
1927
1928 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
1929 vertexMember.name + "'";
1930
1931 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
1932 {
1933 return false;
1934 }
1935 }
1936
1937 return true;
1938}
1939
1940bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
1941{
Cooper Partin1acf4382015-06-12 12:38:57 -07001942#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
1943 const bool validatePrecision = true;
1944#else
1945 const bool validatePrecision = false;
1946#endif
1947
1948 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001949 {
1950 return false;
1951 }
1952
1953 return true;
1954}
1955
1956bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
1957{
1958 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
1959 {
1960 return false;
1961 }
1962
Jamie Madille9cc4692015-02-19 16:00:13 -05001963 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001964 {
Jamie Madillf6113162015-05-07 11:49:21 -04001965 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001966 return false;
1967 }
1968
1969 return true;
1970}
1971
Jamie Madillccdf74b2015-08-18 10:46:12 -04001972bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
1973 const std::vector<const sh::Varying *> &varyings,
1974 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975{
1976 size_t totalComponents = 0;
1977
Jamie Madillccdf74b2015-08-18 10:46:12 -04001978 std::set<std::string> uniqueNames;
1979
1980 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001981 {
1982 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001983 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001984 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001985 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001986 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001987 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001989 infoLog << "Two transform feedback varyings specify the same output variable ("
1990 << tfVaryingName << ").";
1991 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001992 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04001993 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001994
Jamie Madillccdf74b2015-08-18 10:46:12 -04001995 // TODO(jmadill): Investigate implementation limits on D3D11
1996 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04001997 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05001998 componentCount > caps.maxTransformFeedbackSeparateComponents)
1999 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002000 infoLog << "Transform feedback varying's " << varying->name << " components ("
2001 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002002 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003 return false;
2004 }
2005
2006 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007 found = true;
2008 break;
2009 }
2010 }
2011
Jamie Madill89bb70e2015-08-31 14:18:39 -04002012 // TODO(jmadill): investigate if we can support capturing array elements.
2013 if (tfVaryingName.find('[') != std::string::npos)
2014 {
2015 infoLog << "Capture of array elements not currently supported.";
2016 return false;
2017 }
2018
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2020 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002021 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002022 }
2023
Jamie Madillada9ecc2015-08-17 12:53:37 -04002024 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002025 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002026 {
Jamie Madillf6113162015-05-07 11:49:21 -04002027 infoLog << "Transform feedback varying total components (" << totalComponents
2028 << ") exceed the maximum interleaved components ("
2029 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002030 return false;
2031 }
2032
2033 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002034}
2035
Jamie Madillccdf74b2015-08-18 10:46:12 -04002036void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2037{
2038 // Gather the linked varyings that are used for transform feedback, they should all exist.
2039 mData.mTransformFeedbackVaryingVars.clear();
2040 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2041 {
2042 for (const sh::Varying *varying : varyings)
2043 {
2044 if (tfVaryingName == varying->name)
2045 {
2046 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2047 break;
2048 }
2049 }
2050 }
2051}
2052
2053std::vector<const sh::Varying *> Program::getMergedVaryings() const
2054{
2055 std::set<std::string> uniqueNames;
2056 std::vector<const sh::Varying *> varyings;
2057
2058 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2059 {
2060 if (uniqueNames.count(varying.name) == 0)
2061 {
2062 uniqueNames.insert(varying.name);
2063 varyings.push_back(&varying);
2064 }
2065 }
2066
2067 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2068 {
2069 if (uniqueNames.count(varying.name) == 0)
2070 {
2071 uniqueNames.insert(varying.name);
2072 varyings.push_back(&varying);
2073 }
2074 }
2075
2076 return varyings;
2077}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002078
2079void Program::linkOutputVariables()
2080{
2081 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2082 ASSERT(fragmentShader != nullptr);
2083
2084 // Skip this step for GLES2 shaders.
2085 if (fragmentShader->getShaderVersion() == 100)
2086 return;
2087
Jamie Madilla0a9e122015-09-02 15:54:30 -04002088 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002089
2090 // TODO(jmadill): any caps validation here?
2091
2092 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2093 outputVariableIndex++)
2094 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002095 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002096
2097 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2098 if (outputVariable.isBuiltIn())
2099 continue;
2100
2101 // Since multiple output locations must be specified, use 0 for non-specified locations.
2102 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2103
2104 ASSERT(outputVariable.staticUse);
2105
2106 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2107 elementIndex++)
2108 {
2109 const int location = baseLocation + elementIndex;
2110 ASSERT(mData.mOutputVariables.count(location) == 0);
2111 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2112 mData.mOutputVariables[location] =
2113 VariableLocation(outputVariable.name, element, outputVariableIndex);
2114 }
2115 }
2116}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002117
2118bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2119{
2120 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2121 VectorAndSamplerCount vsCounts;
2122
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002123 std::vector<LinkedUniform> samplerUniforms;
2124
Jamie Madill62d31cb2015-09-11 13:25:51 -04002125 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2126 {
2127 if (uniform.staticUse)
2128 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002129 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002130 }
2131 }
2132
2133 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2134 {
2135 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2136 << caps.maxVertexUniformVectors << ").";
2137 return false;
2138 }
2139
2140 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2141 {
2142 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2143 << caps.maxVertexTextureImageUnits << ").";
2144 return false;
2145 }
2146
2147 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2148 VectorAndSamplerCount fsCounts;
2149
2150 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2151 {
2152 if (uniform.staticUse)
2153 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002154 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002155 }
2156 }
2157
2158 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2159 {
2160 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2161 << caps.maxFragmentUniformVectors << ").";
2162 return false;
2163 }
2164
2165 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2166 {
2167 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2168 << caps.maxTextureImageUnits << ").";
2169 return false;
2170 }
2171
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002172 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2173 mSamplerUniformRange.end =
2174 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2175
2176 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2177
Jamie Madill62d31cb2015-09-11 13:25:51 -04002178 return true;
2179}
2180
2181Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002182 const std::string &fullName,
2183 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002184{
2185 VectorAndSamplerCount vectorAndSamplerCount;
2186
2187 if (uniform.isStruct())
2188 {
2189 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2190 {
2191 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2192
2193 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2194 {
2195 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2196 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2197
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002198 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002199 }
2200 }
2201
2202 return vectorAndSamplerCount;
2203 }
2204
2205 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002206 bool isSampler = IsSamplerType(uniform.type);
2207 if (!UniformInList(mData.getUniforms(), fullName) && !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002208 {
2209 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2210 uniform.arraySize, -1,
2211 sh::BlockMemberInfo::getDefaultBlockInfo());
2212 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002213
2214 // Store sampler uniforms separately, so we'll append them to the end of the list.
2215 if (isSampler)
2216 {
2217 samplerUniforms->push_back(linkedUniform);
2218 }
2219 else
2220 {
2221 mData.mUniforms.push_back(linkedUniform);
2222 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002223 }
2224
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002225 unsigned int elementCount = uniform.elementCount();
2226 vectorAndSamplerCount.vectorCount = (VariableRegisterCount(uniform.type) * elementCount);
2227 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002228
2229 return vectorAndSamplerCount;
2230}
2231
2232void Program::gatherInterfaceBlockInfo()
2233{
2234 std::set<std::string> visitedList;
2235
2236 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2237
2238 ASSERT(mData.mUniformBlocks.empty());
2239 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2240 {
2241 // Only 'packed' blocks are allowed to be considered inacive.
2242 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2243 continue;
2244
2245 if (visitedList.count(vertexBlock.name) > 0)
2246 continue;
2247
2248 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2249 visitedList.insert(vertexBlock.name);
2250 }
2251
2252 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2253
2254 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2255 {
2256 // Only 'packed' blocks are allowed to be considered inacive.
2257 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2258 continue;
2259
2260 if (visitedList.count(fragmentBlock.name) > 0)
2261 {
2262 for (gl::UniformBlock &block : mData.mUniformBlocks)
2263 {
2264 if (block.name == fragmentBlock.name)
2265 {
2266 block.fragmentStaticUse = fragmentBlock.staticUse;
2267 }
2268 }
2269
2270 continue;
2271 }
2272
2273 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2274 visitedList.insert(fragmentBlock.name);
2275 }
2276}
2277
Jamie Madill4a3c2342015-10-08 12:58:45 -04002278template <typename VarT>
2279void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2280 const std::string &prefix,
2281 int blockIndex)
2282{
2283 for (const VarT &field : fields)
2284 {
2285 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2286
2287 if (field.isStruct())
2288 {
2289 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2290 {
2291 const std::string uniformElementName =
2292 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2293 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2294 }
2295 }
2296 else
2297 {
2298 // If getBlockMemberInfo returns false, the uniform is optimized out.
2299 sh::BlockMemberInfo memberInfo;
2300 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2301 {
2302 continue;
2303 }
2304
2305 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2306 blockIndex, memberInfo);
2307
2308 // Since block uniforms have no location, we don't need to store them in the uniform
2309 // locations list.
2310 mData.mUniforms.push_back(newUniform);
2311 }
2312 }
2313}
2314
Jamie Madill62d31cb2015-09-11 13:25:51 -04002315void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2316{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002317 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2318 size_t blockSize = 0;
2319
2320 // Don't define this block at all if it's not active in the implementation.
2321 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2322 {
2323 return;
2324 }
2325
2326 // Track the first and last uniform index to determine the range of active uniforms in the
2327 // block.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002328 size_t firstBlockUniformIndex = mData.mUniforms.size();
Jamie Madill4a3c2342015-10-08 12:58:45 -04002329 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002330 size_t lastBlockUniformIndex = mData.mUniforms.size();
2331
2332 std::vector<unsigned int> blockUniformIndexes;
2333 for (size_t blockUniformIndex = firstBlockUniformIndex;
2334 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2335 {
2336 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2337 }
2338
2339 if (interfaceBlock.arraySize > 0)
2340 {
2341 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2342 {
2343 UniformBlock block(interfaceBlock.name, true, arrayElement);
2344 block.memberUniformIndexes = blockUniformIndexes;
2345
2346 if (shaderType == GL_VERTEX_SHADER)
2347 {
2348 block.vertexStaticUse = interfaceBlock.staticUse;
2349 }
2350 else
2351 {
2352 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2353 block.fragmentStaticUse = interfaceBlock.staticUse;
2354 }
2355
Jamie Madill4a3c2342015-10-08 12:58:45 -04002356 // TODO(jmadill): Determine if we can ever have an inactive array element block.
2357 size_t blockElementSize = 0;
2358 if (!mProgram->getUniformBlockSize(block.nameWithArrayIndex(), &blockElementSize))
2359 {
2360 continue;
2361 }
2362
2363 ASSERT(blockElementSize == blockSize);
2364 block.dataSize = static_cast<unsigned int>(blockElementSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002365 mData.mUniformBlocks.push_back(block);
2366 }
2367 }
2368 else
2369 {
2370 UniformBlock block(interfaceBlock.name, false, 0);
2371 block.memberUniformIndexes = blockUniformIndexes;
2372
2373 if (shaderType == GL_VERTEX_SHADER)
2374 {
2375 block.vertexStaticUse = interfaceBlock.staticUse;
2376 }
2377 else
2378 {
2379 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2380 block.fragmentStaticUse = interfaceBlock.staticUse;
2381 }
2382
Jamie Madill4a3c2342015-10-08 12:58:45 -04002383 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002384 mData.mUniformBlocks.push_back(block);
2385 }
2386}
2387
2388template <typename T>
2389void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2390{
2391 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2392 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2393 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2394
2395 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2396 {
2397 // Do a cast conversion for boolean types. From the spec:
2398 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2399 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2400 for (GLsizei component = 0; component < count; ++component)
2401 {
2402 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2403 }
2404 }
2405 else
2406 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002407 // Invalide the validation cache if we modify the sampler data.
2408 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2409 {
2410 mCachedValidateSamplersResult.reset();
2411 }
2412
Jamie Madill62d31cb2015-09-11 13:25:51 -04002413 memcpy(destPointer, v, sizeof(T) * count);
2414 }
2415}
2416
2417template <size_t cols, size_t rows, typename T>
2418void Program::setMatrixUniformInternal(GLint location,
2419 GLsizei count,
2420 GLboolean transpose,
2421 const T *v)
2422{
2423 if (!transpose)
2424 {
2425 setUniformInternal(location, count * cols * rows, v);
2426 return;
2427 }
2428
2429 // Perform a transposing copy.
2430 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2431 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2432 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2433 for (GLsizei element = 0; element < count; ++element)
2434 {
2435 size_t elementOffset = element * rows * cols;
2436
2437 for (size_t row = 0; row < rows; ++row)
2438 {
2439 for (size_t col = 0; col < cols; ++col)
2440 {
2441 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2442 }
2443 }
2444 }
2445}
2446
2447template <typename DestT>
2448void Program::getUniformInternal(GLint location, DestT *dataOut) const
2449{
2450 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2451 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2452
2453 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2454
2455 GLenum componentType = VariableComponentType(uniform.type);
2456 if (componentType == GLTypeToGLenum<DestT>::value)
2457 {
2458 memcpy(dataOut, srcPointer, uniform.getElementSize());
2459 return;
2460 }
2461
2462 int components = VariableComponentCount(uniform.type) * uniform.elementCount();
2463
2464 switch (componentType)
2465 {
2466 case GL_INT:
2467 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2468 break;
2469 case GL_UNSIGNED_INT:
2470 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2471 break;
2472 case GL_BOOL:
2473 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2474 break;
2475 case GL_FLOAT:
2476 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2477 break;
2478 default:
2479 UNREACHABLE();
2480 }
2481}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002482}