blob: 4a99c88fddac3f14bac55158752814f8d20821b8 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Geoff Lang48dcae72014-02-05 16:28:24 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Program.cpp: Implements the gl::Program class. Implements GL program objects
8// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
9
Geoff Lang2b5420c2014-11-19 14:20:15 -050010#include "libANGLE/Program.h"
Jamie Madill437d2662014-12-05 14:23:35 -050011
Jamie Madill9e0478f2015-01-13 11:13:54 -050012#include <algorithm>
13
Jamie Madill80a6fc02015-08-21 16:53:16 -040014#include "common/BitSetIterator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/debug.h"
16#include "common/platform.h"
17#include "common/utilities.h"
18#include "common/version.h"
19#include "compiler/translator/blocklayout.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050020#include "libANGLE/Data.h"
Jamie Madill437d2662014-12-05 14:23:35 -050021#include "libANGLE/ResourceManager.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050022#include "libANGLE/features.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050023#include "libANGLE/renderer/Renderer.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050024#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill62d31cb2015-09-11 13:25:51 -040025#include "libANGLE/queryconversions.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050026
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027namespace gl
28{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000029
Geoff Lang7dd2e102014-11-10 15:19:26 -050030namespace
31{
32
Jamie Madill62d31cb2015-09-11 13:25:51 -040033void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
34{
35 stream->writeInt(var.type);
36 stream->writeInt(var.precision);
37 stream->writeString(var.name);
38 stream->writeString(var.mappedName);
39 stream->writeInt(var.arraySize);
40 stream->writeInt(var.staticUse);
41 stream->writeString(var.structName);
42 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050043}
44
Jamie Madill62d31cb2015-09-11 13:25:51 -040045void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
46{
47 var->type = stream->readInt<GLenum>();
48 var->precision = stream->readInt<GLenum>();
49 var->name = stream->readString();
50 var->mappedName = stream->readString();
51 var->arraySize = stream->readInt<unsigned int>();
52 var->staticUse = stream->readBool();
53 var->structName = stream->readString();
54}
55
Jamie Madill62d31cb2015-09-11 13:25:51 -040056// This simplified cast function doesn't need to worry about advanced concepts like
57// depth range values, or casting to bool.
58template <typename DestT, typename SrcT>
59DestT UniformStateQueryCast(SrcT value);
60
61// From-Float-To-Integer Casts
62template <>
63GLint UniformStateQueryCast(GLfloat value)
64{
65 return clampCast<GLint>(roundf(value));
66}
67
68template <>
69GLuint UniformStateQueryCast(GLfloat value)
70{
71 return clampCast<GLuint>(roundf(value));
72}
73
74// From-Integer-to-Integer Casts
75template <>
76GLint UniformStateQueryCast(GLuint value)
77{
78 return clampCast<GLint>(value);
79}
80
81template <>
82GLuint UniformStateQueryCast(GLint value)
83{
84 return clampCast<GLuint>(value);
85}
86
87// From-Boolean-to-Anything Casts
88template <>
89GLfloat UniformStateQueryCast(GLboolean value)
90{
91 return (value == GL_TRUE ? 1.0f : 0.0f);
92}
93
94template <>
95GLint UniformStateQueryCast(GLboolean value)
96{
97 return (value == GL_TRUE ? 1 : 0);
98}
99
100template <>
101GLuint UniformStateQueryCast(GLboolean value)
102{
103 return (value == GL_TRUE ? 1u : 0u);
104}
105
106// Default to static_cast
107template <typename DestT, typename SrcT>
108DestT UniformStateQueryCast(SrcT value)
109{
110 return static_cast<DestT>(value);
111}
112
113template <typename SrcT, typename DestT>
114void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
115{
116 for (int comp = 0; comp < components; ++comp)
117 {
118 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
119 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
120 size_t offset = comp * 4;
121 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
122 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
123 }
124}
125
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400126bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
127{
128 for (const LinkedUniform &uniform : list)
129 {
130 if (uniform.name == name)
131 return true;
132 }
133
134 return false;
135}
136
Jamie Madill62d31cb2015-09-11 13:25:51 -0400137} // anonymous namespace
138
Jamie Madill4a3c2342015-10-08 12:58:45 -0400139const char *const g_fakepath = "C:\\fakepath";
140
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000141AttributeBindings::AttributeBindings()
142{
143}
144
145AttributeBindings::~AttributeBindings()
146{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000147}
148
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400149InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000150{
151}
152
153InfoLog::~InfoLog()
154{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000155}
156
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400157size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000158{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400159 const std::string &logString = mStream.str();
160 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000161}
162
Geoff Lange1a27752015-10-05 13:16:04 -0400163void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000164{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400165 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000166
167 if (bufSize > 0)
168 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400169 const std::string str(mStream.str());
170
171 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000172 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400173 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
174 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000175 }
176
177 infoLog[index] = '\0';
178 }
179
180 if (length)
181 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400182 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000183 }
184}
185
186// append a santized message to the program info log.
187// The D3D compiler includes a fake file path in some of the warning or error
188// messages, so lets remove all occurrences of this fake file path from the log.
189void InfoLog::appendSanitized(const char *message)
190{
191 std::string msg(message);
192
193 size_t found;
194 do
195 {
196 found = msg.find(g_fakepath);
197 if (found != std::string::npos)
198 {
199 msg.erase(found, strlen(g_fakepath));
200 }
201 }
202 while (found != std::string::npos);
203
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400204 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000205}
206
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000207void InfoLog::reset()
208{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000209}
210
Geoff Lang7dd2e102014-11-10 15:19:26 -0500211VariableLocation::VariableLocation()
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400212 : name(),
213 element(0),
214 index(0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000215{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500216}
217
218VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index)
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400219 : name(name),
220 element(element),
221 index(index)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500222{
223}
224
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400225Program::Data::Data()
226 : mAttachedFragmentShader(nullptr),
227 mAttachedVertexShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500228 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
229 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400230{
231}
232
233Program::Data::~Data()
234{
235 if (mAttachedVertexShader != nullptr)
236 {
237 mAttachedVertexShader->release();
238 }
239
240 if (mAttachedFragmentShader != nullptr)
241 {
242 mAttachedFragmentShader->release();
243 }
244}
245
Jamie Madill62d31cb2015-09-11 13:25:51 -0400246const LinkedUniform *Program::Data::getUniformByName(const std::string &name) const
247{
248 for (const LinkedUniform &linkedUniform : mUniforms)
249 {
250 if (linkedUniform.name == name)
251 {
252 return &linkedUniform;
253 }
254 }
255
256 return nullptr;
257}
258
259GLint Program::Data::getUniformLocation(const std::string &name) const
260{
261 size_t subscript = GL_INVALID_INDEX;
262 std::string baseName = gl::ParseUniformName(name, &subscript);
263
264 for (size_t location = 0; location < mUniformLocations.size(); ++location)
265 {
266 const VariableLocation &uniformLocation = mUniformLocations[location];
267 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
268
269 if (uniform.name == baseName)
270 {
271 if ((uniform.isArray() && uniformLocation.element == subscript) ||
272 (subscript == GL_INVALID_INDEX))
273 {
274 return static_cast<GLint>(location);
275 }
276 }
277 }
278
279 return -1;
280}
281
282GLuint Program::Data::getUniformIndex(const std::string &name) const
283{
284 size_t subscript = GL_INVALID_INDEX;
285 std::string baseName = gl::ParseUniformName(name, &subscript);
286
287 // The app is not allowed to specify array indices other than 0 for arrays of basic types
288 if (subscript != 0 && subscript != GL_INVALID_INDEX)
289 {
290 return GL_INVALID_INDEX;
291 }
292
293 for (size_t index = 0; index < mUniforms.size(); index++)
294 {
295 const LinkedUniform &uniform = mUniforms[index];
296 if (uniform.name == baseName)
297 {
298 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
299 {
300 return static_cast<GLuint>(index);
301 }
302 }
303 }
304
305 return GL_INVALID_INDEX;
306}
307
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400308Program::Program(rx::ImplFactory *factory, ResourceManager *manager, GLuint handle)
309 : mProgram(factory->createProgram(mData)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400310 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500311 mLinked(false),
312 mDeleteStatus(false),
313 mRefCount(0),
314 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400315 mHandle(handle),
316 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500317{
318 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000319
320 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500321 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322}
323
324Program::~Program()
325{
326 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000327
Geoff Lang7dd2e102014-11-10 15:19:26 -0500328 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329}
330
331bool Program::attachShader(Shader *shader)
332{
333 if (shader->getType() == GL_VERTEX_SHADER)
334 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400335 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 {
337 return false;
338 }
339
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400340 mData.mAttachedVertexShader = shader;
341 mData.mAttachedVertexShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342 }
343 else if (shader->getType() == GL_FRAGMENT_SHADER)
344 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400345 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346 {
347 return false;
348 }
349
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400350 mData.mAttachedFragmentShader = shader;
351 mData.mAttachedFragmentShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352 }
353 else UNREACHABLE();
354
355 return true;
356}
357
358bool Program::detachShader(Shader *shader)
359{
360 if (shader->getType() == GL_VERTEX_SHADER)
361 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400362 if (mData.mAttachedVertexShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363 {
364 return false;
365 }
366
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400367 shader->release();
368 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369 }
370 else if (shader->getType() == GL_FRAGMENT_SHADER)
371 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400372 if (mData.mAttachedFragmentShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373 {
374 return false;
375 }
376
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400377 shader->release();
378 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000379 }
380 else UNREACHABLE();
381
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382 return true;
383}
384
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000385int Program::getAttachedShadersCount() const
386{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400387 return (mData.mAttachedVertexShader ? 1 : 0) + (mData.mAttachedFragmentShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000388}
389
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000390void AttributeBindings::bindAttributeLocation(GLuint index, const char *name)
391{
392 if (index < MAX_VERTEX_ATTRIBS)
393 {
394 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
395 {
396 mAttributeBinding[i].erase(name);
397 }
398
399 mAttributeBinding[index].insert(name);
400 }
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000401}
402
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403void Program::bindAttributeLocation(GLuint index, const char *name)
404{
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000405 mAttributeBindings.bindAttributeLocation(index, name);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406}
407
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
409// compiling them into binaries, determining the attribute mappings, and collecting
410// a list of uniforms
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400411Error Program::link(const gl::Data &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000412{
413 unlink(false);
414
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000415 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000416 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000417
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400418 if (!mData.mAttachedFragmentShader || !mData.mAttachedFragmentShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500419 {
420 return Error(GL_NO_ERROR);
421 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400422 ASSERT(mData.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500423
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400424 if (!mData.mAttachedVertexShader || !mData.mAttachedVertexShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500425 {
426 return Error(GL_NO_ERROR);
427 }
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400428 ASSERT(mData.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500429
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400430 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mData.mAttachedVertexShader))
Jamie Madill437d2662014-12-05 14:23:35 -0500431 {
432 return Error(GL_NO_ERROR);
433 }
434
Jamie Madillada9ecc2015-08-17 12:53:37 -0400435 if (!linkVaryings(mInfoLog, mData.mAttachedVertexShader, mData.mAttachedFragmentShader))
436 {
437 return Error(GL_NO_ERROR);
438 }
439
Jamie Madillea918db2015-08-18 14:48:59 -0400440 if (!linkUniforms(mInfoLog, *data.caps))
441 {
442 return Error(GL_NO_ERROR);
443 }
444
Jamie Madille473dee2015-08-18 14:49:01 -0400445 if (!linkUniformBlocks(mInfoLog, *data.caps))
446 {
447 return Error(GL_NO_ERROR);
448 }
449
Jamie Madillccdf74b2015-08-18 10:46:12 -0400450 const auto &mergedVaryings = getMergedVaryings();
451
452 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, *data.caps))
453 {
454 return Error(GL_NO_ERROR);
455 }
456
Jamie Madill80a6fc02015-08-21 16:53:16 -0400457 linkOutputVariables();
458
Jamie Madillf5f4ad22015-09-02 18:32:38 +0000459 rx::LinkResult result = mProgram->link(data, mInfoLog);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500460 if (result.error.isError() || !result.linkSuccess)
461 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500462 return result.error;
463 }
464
Jamie Madillccdf74b2015-08-18 10:46:12 -0400465 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400466 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400467
Geoff Lang7dd2e102014-11-10 15:19:26 -0500468 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400469 return gl::Error(GL_NO_ERROR);
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000470}
471
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000472int AttributeBindings::getAttributeBinding(const std::string &name) const
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000473{
474 for (int location = 0; location < MAX_VERTEX_ATTRIBS; location++)
475 {
476 if (mAttributeBinding[location].find(name) != mAttributeBinding[location].end())
477 {
478 return location;
479 }
480 }
481
482 return -1;
483}
484
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000485// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486void Program::unlink(bool destroy)
487{
488 if (destroy) // Object being destructed
489 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400490 if (mData.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400492 mData.mAttachedFragmentShader->release();
493 mData.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494 }
495
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400496 if (mData.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400498 mData.mAttachedVertexShader->release();
499 mData.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501 }
502
Jamie Madillc349ec02015-08-21 16:53:12 -0400503 mData.mAttributes.clear();
Jamie Madill63805b42015-08-25 13:17:39 -0400504 mData.mActiveAttribLocationsMask.reset();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400505 mData.mTransformFeedbackVaryingVars.clear();
Jamie Madill62d31cb2015-09-11 13:25:51 -0400506 mData.mUniforms.clear();
507 mData.mUniformLocations.clear();
508 mData.mUniformBlocks.clear();
Jamie Madill80a6fc02015-08-21 16:53:16 -0400509 mData.mOutputVariables.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500510
Geoff Lang7dd2e102014-11-10 15:19:26 -0500511 mValidated = false;
512
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000513 mLinked = false;
514}
515
Geoff Lange1a27752015-10-05 13:16:04 -0400516bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000517{
518 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519}
520
Geoff Lang7dd2e102014-11-10 15:19:26 -0500521Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000522{
523 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000524
Geoff Lang7dd2e102014-11-10 15:19:26 -0500525#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
526 return Error(GL_NO_ERROR);
527#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400528 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
529 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000530 {
Jamie Madillf6113162015-05-07 11:49:21 -0400531 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500532 return Error(GL_NO_ERROR);
533 }
534
Geoff Langc46cc2f2015-10-01 17:16:20 -0400535 BinaryInputStream stream(binary, length);
536
Geoff Lang7dd2e102014-11-10 15:19:26 -0500537 int majorVersion = stream.readInt<int>();
538 int minorVersion = stream.readInt<int>();
539 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
540 {
Jamie Madillf6113162015-05-07 11:49:21 -0400541 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500542 return Error(GL_NO_ERROR);
543 }
544
545 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
546 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
547 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
548 {
Jamie Madillf6113162015-05-07 11:49:21 -0400549 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500550 return Error(GL_NO_ERROR);
551 }
552
Jamie Madill63805b42015-08-25 13:17:39 -0400553 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
554 "Too many vertex attribs for mask");
555 mData.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500556
Jamie Madill3da79b72015-04-27 11:09:17 -0400557 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400558 ASSERT(mData.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400559 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
560 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400561 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400562 LoadShaderVar(&stream, &attrib);
563 attrib.location = stream.readInt<int>();
Jamie Madillc349ec02015-08-21 16:53:12 -0400564 mData.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400565 }
566
Jamie Madill62d31cb2015-09-11 13:25:51 -0400567 unsigned int uniformCount = stream.readInt<unsigned int>();
568 ASSERT(mData.mUniforms.empty());
569 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
570 {
571 LinkedUniform uniform;
572 LoadShaderVar(&stream, &uniform);
573
574 uniform.blockIndex = stream.readInt<int>();
575 uniform.blockInfo.offset = stream.readInt<int>();
576 uniform.blockInfo.arrayStride = stream.readInt<int>();
577 uniform.blockInfo.matrixStride = stream.readInt<int>();
578 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
579
580 mData.mUniforms.push_back(uniform);
581 }
582
583 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
584 ASSERT(mData.mUniformLocations.empty());
585 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
586 uniformIndexIndex++)
587 {
588 VariableLocation variable;
589 stream.readString(&variable.name);
590 stream.readInt(&variable.element);
591 stream.readInt(&variable.index);
592
593 mData.mUniformLocations.push_back(variable);
594 }
595
596 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
597 ASSERT(mData.mUniformBlocks.empty());
598 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
599 ++uniformBlockIndex)
600 {
601 UniformBlock uniformBlock;
602 stream.readString(&uniformBlock.name);
603 stream.readBool(&uniformBlock.isArray);
604 stream.readInt(&uniformBlock.arrayElement);
605 stream.readInt(&uniformBlock.dataSize);
606 stream.readBool(&uniformBlock.vertexStaticUse);
607 stream.readBool(&uniformBlock.fragmentStaticUse);
608
609 unsigned int numMembers = stream.readInt<unsigned int>();
610 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
611 {
612 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
613 }
614
Jamie Madill62d31cb2015-09-11 13:25:51 -0400615 mData.mUniformBlocks.push_back(uniformBlock);
616 }
617
Brandon Jones1048ea72015-10-06 15:34:52 -0700618 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
619 ASSERT(mData.mTransformFeedbackVaryingVars.empty());
620 for (unsigned int transformFeedbackVaryingIndex = 0;
621 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
622 ++transformFeedbackVaryingIndex)
623 {
624 sh::Varying varying;
625 stream.readInt(&varying.arraySize);
626 stream.readInt(&varying.type);
627 stream.readString(&varying.name);
628
629 mData.mTransformFeedbackVaryingVars.push_back(varying);
630 }
631
Jamie Madillada9ecc2015-08-17 12:53:37 -0400632 stream.readInt(&mData.mTransformFeedbackBufferMode);
633
Jamie Madill80a6fc02015-08-21 16:53:16 -0400634 unsigned int outputVarCount = stream.readInt<unsigned int>();
635 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
636 {
637 int locationIndex = stream.readInt<int>();
638 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400639 stream.readInt(&locationData.element);
640 stream.readInt(&locationData.index);
641 stream.readString(&locationData.name);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400642 mData.mOutputVariables[locationIndex] = locationData;
643 }
644
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400645 stream.readInt(&mSamplerUniformRange.start);
646 stream.readInt(&mSamplerUniformRange.end);
647
Geoff Lang7dd2e102014-11-10 15:19:26 -0500648 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
649 if (result.error.isError() || !result.linkSuccess)
650 {
Geoff Langb543aff2014-09-30 14:52:54 -0400651 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000652 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000653
Geoff Lang7dd2e102014-11-10 15:19:26 -0500654 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400655 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500656#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
657}
658
659Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
660{
661 if (binaryFormat)
662 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400663 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500664 }
665
666 BinaryOutputStream stream;
667
Geoff Lang7dd2e102014-11-10 15:19:26 -0500668 stream.writeInt(ANGLE_MAJOR_VERSION);
669 stream.writeInt(ANGLE_MINOR_VERSION);
670 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
671
Jamie Madill63805b42015-08-25 13:17:39 -0400672 stream.writeInt(mData.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500673
Jamie Madillc349ec02015-08-21 16:53:12 -0400674 stream.writeInt(mData.mAttributes.size());
675 for (const sh::Attribute &attrib : mData.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400676 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400677 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400678 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400679 }
680
681 stream.writeInt(mData.mUniforms.size());
682 for (const gl::LinkedUniform &uniform : mData.mUniforms)
683 {
684 WriteShaderVar(&stream, uniform);
685
686 // FIXME: referenced
687
688 stream.writeInt(uniform.blockIndex);
689 stream.writeInt(uniform.blockInfo.offset);
690 stream.writeInt(uniform.blockInfo.arrayStride);
691 stream.writeInt(uniform.blockInfo.matrixStride);
692 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
693 }
694
695 stream.writeInt(mData.mUniformLocations.size());
696 for (const auto &variable : mData.mUniformLocations)
697 {
698 stream.writeString(variable.name);
699 stream.writeInt(variable.element);
700 stream.writeInt(variable.index);
701 }
702
703 stream.writeInt(mData.mUniformBlocks.size());
704 for (const UniformBlock &uniformBlock : mData.mUniformBlocks)
705 {
706 stream.writeString(uniformBlock.name);
707 stream.writeInt(uniformBlock.isArray);
708 stream.writeInt(uniformBlock.arrayElement);
709 stream.writeInt(uniformBlock.dataSize);
710
711 stream.writeInt(uniformBlock.vertexStaticUse);
712 stream.writeInt(uniformBlock.fragmentStaticUse);
713
714 stream.writeInt(uniformBlock.memberUniformIndexes.size());
715 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
716 {
717 stream.writeInt(memberUniformIndex);
718 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400719 }
720
Brandon Jones1048ea72015-10-06 15:34:52 -0700721 stream.writeInt(mData.mTransformFeedbackVaryingVars.size());
722 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
723 {
724 stream.writeInt(varying.arraySize);
725 stream.writeInt(varying.type);
726 stream.writeString(varying.name);
727 }
728
Jamie Madillada9ecc2015-08-17 12:53:37 -0400729 stream.writeInt(mData.mTransformFeedbackBufferMode);
730
Jamie Madill80a6fc02015-08-21 16:53:16 -0400731 stream.writeInt(mData.mOutputVariables.size());
732 for (const auto &outputPair : mData.mOutputVariables)
733 {
734 stream.writeInt(outputPair.first);
735 stream.writeInt(outputPair.second.element);
736 stream.writeInt(outputPair.second.index);
737 stream.writeString(outputPair.second.name);
738 }
739
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400740 stream.writeInt(mSamplerUniformRange.start);
741 stream.writeInt(mSamplerUniformRange.end);
742
Geoff Lang7dd2e102014-11-10 15:19:26 -0500743 gl::Error error = mProgram->save(&stream);
744 if (error.isError())
745 {
746 return error;
747 }
748
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700749 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500750 const void *streamData = stream.data();
751
752 if (streamLength > bufSize)
753 {
754 if (length)
755 {
756 *length = 0;
757 }
758
759 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
760 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
761 // sizes and then copy it.
762 return Error(GL_INVALID_OPERATION);
763 }
764
765 if (binary)
766 {
767 char *ptr = reinterpret_cast<char*>(binary);
768
769 memcpy(ptr, streamData, streamLength);
770 ptr += streamLength;
771
772 ASSERT(ptr - streamLength == binary);
773 }
774
775 if (length)
776 {
777 *length = streamLength;
778 }
779
780 return Error(GL_NO_ERROR);
781}
782
783GLint Program::getBinaryLength() const
784{
785 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400786 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500787 if (error.isError())
788 {
789 return 0;
790 }
791
792 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000793}
794
Geoff Langc5629752015-12-07 16:29:04 -0500795void Program::setBinaryRetrievableHint(bool retrievable)
796{
797 // TODO(jmadill) : replace with dirty bits
798 mProgram->setBinaryRetrievableHint(retrievable);
799 mData.mBinaryRetrieveableHint = retrievable;
800}
801
802bool Program::getBinaryRetrievableHint() const
803{
804 return mData.mBinaryRetrieveableHint;
805}
806
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000807void Program::release()
808{
809 mRefCount--;
810
811 if (mRefCount == 0 && mDeleteStatus)
812 {
813 mResourceManager->deleteProgram(mHandle);
814 }
815}
816
817void Program::addRef()
818{
819 mRefCount++;
820}
821
822unsigned int Program::getRefCount() const
823{
824 return mRefCount;
825}
826
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000827int Program::getInfoLogLength() const
828{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400829 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000830}
831
Geoff Lange1a27752015-10-05 13:16:04 -0400832void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000833{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000834 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000835}
836
Geoff Lange1a27752015-10-05 13:16:04 -0400837void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000838{
839 int total = 0;
840
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400841 if (mData.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000842 {
843 if (total < maxCount)
844 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400845 shaders[total] = mData.mAttachedVertexShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000846 }
847
848 total++;
849 }
850
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400851 if (mData.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000852 {
853 if (total < maxCount)
854 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400855 shaders[total] = mData.mAttachedFragmentShader->getHandle();
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000856 }
857
858 total++;
859 }
860
861 if (count)
862 {
863 *count = total;
864 }
865}
866
Geoff Lange1a27752015-10-05 13:16:04 -0400867GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500868{
Jamie Madillc349ec02015-08-21 16:53:12 -0400869 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500870 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400871 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500872 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400873 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500874 }
875 }
876
Austin Kinrossb8af7232015-03-16 22:33:25 -0700877 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500878}
879
Jamie Madill63805b42015-08-25 13:17:39 -0400880bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400881{
Olli Etuaho401d9fe2015-08-26 10:19:59 +0300882 ASSERT(attribLocation < mData.mActiveAttribLocationsMask.size());
Jamie Madill63805b42015-08-25 13:17:39 -0400883 return mData.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500884}
885
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000886void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
887{
Jamie Madillc349ec02015-08-21 16:53:12 -0400888 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000889 {
890 if (bufsize > 0)
891 {
892 name[0] = '\0';
893 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500894
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000895 if (length)
896 {
897 *length = 0;
898 }
899
900 *type = GL_NONE;
901 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400902 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000903 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400904
905 size_t attributeIndex = 0;
906
907 for (const sh::Attribute &attribute : mData.mAttributes)
908 {
909 // Skip over inactive attributes
910 if (attribute.staticUse)
911 {
912 if (static_cast<size_t>(index) == attributeIndex)
913 {
914 break;
915 }
916 attributeIndex++;
917 }
918 }
919
920 ASSERT(index == attributeIndex && attributeIndex < mData.mAttributes.size());
921 const sh::Attribute &attrib = mData.mAttributes[attributeIndex];
922
923 if (bufsize > 0)
924 {
925 const char *string = attrib.name.c_str();
926
927 strncpy(name, string, bufsize);
928 name[bufsize - 1] = '\0';
929
930 if (length)
931 {
932 *length = static_cast<GLsizei>(strlen(name));
933 }
934 }
935
936 // Always a single 'type' instance
937 *size = 1;
938 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000939}
940
Geoff Lange1a27752015-10-05 13:16:04 -0400941GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000942{
Jamie Madillc349ec02015-08-21 16:53:12 -0400943 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400944 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400945 return 0;
946 }
947
948 GLint count = 0;
949
950 for (const sh::Attribute &attrib : mData.mAttributes)
951 {
952 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000953 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500954
955 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000956}
957
Geoff Lange1a27752015-10-05 13:16:04 -0400958GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000959{
Jamie Madillc349ec02015-08-21 16:53:12 -0400960 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400961 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400962 return 0;
963 }
964
965 size_t maxLength = 0;
966
967 for (const sh::Attribute &attrib : mData.mAttributes)
968 {
969 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500970 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400971 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500972 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000973 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500974
Jamie Madillc349ec02015-08-21 16:53:12 -0400975 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500976}
977
Geoff Lang7dd2e102014-11-10 15:19:26 -0500978GLint Program::getFragDataLocation(const std::string &name) const
979{
980 std::string baseName(name);
981 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400982 for (auto outputPair : mData.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000983 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400984 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500985 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
986 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400987 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500988 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000989 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500990 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000991}
992
Geoff Lange1a27752015-10-05 13:16:04 -0400993void Program::getActiveUniform(GLuint index,
994 GLsizei bufsize,
995 GLsizei *length,
996 GLint *size,
997 GLenum *type,
998 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000999{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001000 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001001 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001002 // index must be smaller than getActiveUniformCount()
1003 ASSERT(index < mData.mUniforms.size());
1004 const LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001005
1006 if (bufsize > 0)
1007 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001008 std::string string = uniform.name;
1009 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001010 {
1011 string += "[0]";
1012 }
1013
1014 strncpy(name, string.c_str(), bufsize);
1015 name[bufsize - 1] = '\0';
1016
1017 if (length)
1018 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001019 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001020 }
1021 }
1022
Jamie Madill62d31cb2015-09-11 13:25:51 -04001023 *size = uniform.elementCount();
1024 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001025 }
1026 else
1027 {
1028 if (bufsize > 0)
1029 {
1030 name[0] = '\0';
1031 }
1032
1033 if (length)
1034 {
1035 *length = 0;
1036 }
1037
1038 *size = 0;
1039 *type = GL_NONE;
1040 }
1041}
1042
Geoff Lange1a27752015-10-05 13:16:04 -04001043GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001044{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001045 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001046 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001047 return static_cast<GLint>(mData.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001048 }
1049 else
1050 {
1051 return 0;
1052 }
1053}
1054
Geoff Lange1a27752015-10-05 13:16:04 -04001055GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001056{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001057 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001058
1059 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001060 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001061 for (const LinkedUniform &uniform : mData.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001062 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001063 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001064 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001065 size_t length = uniform.name.length() + 1u;
1066 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001067 {
1068 length += 3; // Counting in "[0]".
1069 }
1070 maxLength = std::max(length, maxLength);
1071 }
1072 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001073 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001074
Jamie Madill62d31cb2015-09-11 13:25:51 -04001075 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001076}
1077
1078GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1079{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001080 ASSERT(static_cast<size_t>(index) < mData.mUniforms.size());
1081 const gl::LinkedUniform &uniform = mData.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001082 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001083 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001084 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1085 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1086 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1087 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1088 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1089 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1090 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1091 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1092 default:
1093 UNREACHABLE();
1094 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001095 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001096 return 0;
1097}
1098
1099bool Program::isValidUniformLocation(GLint location) const
1100{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001101 ASSERT(rx::IsIntegerCastSafe<GLint>(mData.mUniformLocations.size()));
1102 return (location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001103}
1104
Jamie Madill62d31cb2015-09-11 13:25:51 -04001105const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001106{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001107 ASSERT(location >= 0 && static_cast<size_t>(location) < mData.mUniformLocations.size());
1108 return mData.mUniforms[mData.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001109}
1110
Jamie Madill62d31cb2015-09-11 13:25:51 -04001111GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001112{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001113 return mData.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001114}
1115
Jamie Madill62d31cb2015-09-11 13:25:51 -04001116GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001117{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001118 return mData.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001119}
1120
1121void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1122{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001123 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001124 mProgram->setUniform1fv(location, count, v);
1125}
1126
1127void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1128{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001129 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001130 mProgram->setUniform2fv(location, count, v);
1131}
1132
1133void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1134{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001135 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001136 mProgram->setUniform3fv(location, count, v);
1137}
1138
1139void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1140{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001141 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142 mProgram->setUniform4fv(location, count, v);
1143}
1144
1145void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1146{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001147 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001148 mProgram->setUniform1iv(location, count, v);
1149}
1150
1151void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1152{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001153 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001154 mProgram->setUniform2iv(location, count, v);
1155}
1156
1157void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1158{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001159 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001160 mProgram->setUniform3iv(location, count, v);
1161}
1162
1163void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1164{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001165 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001166 mProgram->setUniform4iv(location, count, v);
1167}
1168
1169void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1170{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001171 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001172 mProgram->setUniform1uiv(location, count, v);
1173}
1174
1175void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1176{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001177 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001178 mProgram->setUniform2uiv(location, count, v);
1179}
1180
1181void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1182{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001183 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001184 mProgram->setUniform3uiv(location, count, v);
1185}
1186
1187void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1188{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001189 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001190 mProgram->setUniform4uiv(location, count, v);
1191}
1192
1193void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1194{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001195 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001196 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1197}
1198
1199void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1200{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001201 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001202 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1203}
1204
1205void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1206{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001207 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001208 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1209}
1210
1211void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1212{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001213 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001214 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1215}
1216
1217void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1218{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001219 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001220 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1221}
1222
1223void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1224{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001225 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001226 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1227}
1228
1229void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1230{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001231 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001232 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1233}
1234
1235void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1236{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001237 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001238 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1239}
1240
1241void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1242{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001243 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001244 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1245}
1246
Geoff Lange1a27752015-10-05 13:16:04 -04001247void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001248{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001249 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001250}
1251
Geoff Lange1a27752015-10-05 13:16:04 -04001252void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001254 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001255}
1256
Geoff Lange1a27752015-10-05 13:16:04 -04001257void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001258{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001259 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001260}
1261
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001262void Program::flagForDeletion()
1263{
1264 mDeleteStatus = true;
1265}
1266
1267bool Program::isFlaggedForDeletion() const
1268{
1269 return mDeleteStatus;
1270}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001271
Brandon Jones43a53e22014-08-28 16:23:22 -07001272void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001273{
1274 mInfoLog.reset();
1275
Geoff Lang7dd2e102014-11-10 15:19:26 -05001276 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001277 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001278 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001279 }
1280 else
1281 {
Jamie Madillf6113162015-05-07 11:49:21 -04001282 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001283 }
1284}
1285
Geoff Lang7dd2e102014-11-10 15:19:26 -05001286bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1287{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001288 // Skip cache if we're using an infolog, so we get the full error.
1289 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1290 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1291 {
1292 return mCachedValidateSamplersResult.value();
1293 }
1294
1295 if (mTextureUnitTypesCache.empty())
1296 {
1297 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1298 }
1299 else
1300 {
1301 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1302 }
1303
1304 // if any two active samplers in a program are of different types, but refer to the same
1305 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1306 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1307 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1308 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1309 {
1310 const LinkedUniform &uniform = mData.mUniforms[samplerIndex];
1311 ASSERT(uniform.isSampler());
1312
1313 if (!uniform.staticUse)
1314 continue;
1315
1316 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1317 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1318
1319 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1320 {
1321 GLuint textureUnit = dataPtr[arrayElement];
1322
1323 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1324 {
1325 if (infoLog)
1326 {
1327 (*infoLog) << "Sampler uniform (" << textureUnit
1328 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1329 << caps.maxCombinedTextureImageUnits << ")";
1330 }
1331
1332 mCachedValidateSamplersResult = false;
1333 return false;
1334 }
1335
1336 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1337 {
1338 if (textureType != mTextureUnitTypesCache[textureUnit])
1339 {
1340 if (infoLog)
1341 {
1342 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1343 "image unit ("
1344 << textureUnit << ").";
1345 }
1346
1347 mCachedValidateSamplersResult = false;
1348 return false;
1349 }
1350 }
1351 else
1352 {
1353 mTextureUnitTypesCache[textureUnit] = textureType;
1354 }
1355 }
1356 }
1357
1358 mCachedValidateSamplersResult = true;
1359 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001360}
1361
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001362bool Program::isValidated() const
1363{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364 return mValidated;
1365}
1366
Geoff Lange1a27752015-10-05 13:16:04 -04001367GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001369 return static_cast<GLuint>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001370}
1371
1372void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1373{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001374 ASSERT(uniformBlockIndex <
1375 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001376
Jamie Madill62d31cb2015-09-11 13:25:51 -04001377 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001378
1379 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001380 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001381 std::string string = uniformBlock.name;
1382
Jamie Madill62d31cb2015-09-11 13:25:51 -04001383 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001385 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001386 }
1387
1388 strncpy(uniformBlockName, string.c_str(), bufSize);
1389 uniformBlockName[bufSize - 1] = '\0';
1390
1391 if (length)
1392 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001393 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001394 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001395 }
1396}
1397
Geoff Lang7dd2e102014-11-10 15:19:26 -05001398void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001399{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001400 ASSERT(uniformBlockIndex <
1401 mData.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402
Jamie Madill62d31cb2015-09-11 13:25:51 -04001403 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001404
1405 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001406 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001407 case GL_UNIFORM_BLOCK_DATA_SIZE:
1408 *params = static_cast<GLint>(uniformBlock.dataSize);
1409 break;
1410 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001411 *params =
1412 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001413 break;
1414 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1415 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1416 break;
1417 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1418 {
1419 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1420 {
1421 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1422 }
1423 }
1424 break;
1425 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001426 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427 break;
1428 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001429 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001430 break;
1431 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001432 }
1433}
1434
Geoff Lange1a27752015-10-05 13:16:04 -04001435GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001436{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437 int maxLength = 0;
1438
1439 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001440 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001441 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001442 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1443 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001444 const UniformBlock &uniformBlock = mData.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445 if (!uniformBlock.name.empty())
1446 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001447 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448
1449 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001450 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001451
1452 maxLength = std::max(length + arrayLength, maxLength);
1453 }
1454 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001455 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456
1457 return maxLength;
1458}
1459
Geoff Lange1a27752015-10-05 13:16:04 -04001460GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001461{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001462 size_t subscript = GL_INVALID_INDEX;
1463 std::string baseName = gl::ParseUniformName(name, &subscript);
1464
1465 unsigned int numUniformBlocks = static_cast<unsigned int>(mData.mUniformBlocks.size());
1466 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1467 {
1468 const gl::UniformBlock &uniformBlock = mData.mUniformBlocks[blockIndex];
1469 if (uniformBlock.name == baseName)
1470 {
1471 const bool arrayElementZero =
1472 (subscript == GL_INVALID_INDEX &&
1473 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1474 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1475 {
1476 return blockIndex;
1477 }
1478 }
1479 }
1480
1481 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001482}
1483
Jamie Madill62d31cb2015-09-11 13:25:51 -04001484const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001485{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001486 ASSERT(index < static_cast<GLuint>(mData.mUniformBlocks.size()));
1487 return mData.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001488}
1489
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001490void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1491{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001492 mData.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001493 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001494}
1495
1496GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1497{
Jamie Madilld1fe1642015-08-21 16:26:04 -04001498 return mData.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001499}
1500
1501void Program::resetUniformBlockBindings()
1502{
1503 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1504 {
Jamie Madilld1fe1642015-08-21 16:26:04 -04001505 mData.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001506 }
Geoff Lang5d124a62015-09-15 13:03:27 -04001507 mData.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001508}
1509
Geoff Lang48dcae72014-02-05 16:28:24 -05001510void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1511{
Jamie Madillccdf74b2015-08-18 10:46:12 -04001512 mData.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001513 for (GLsizei i = 0; i < count; i++)
1514 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001515 mData.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001516 }
1517
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001518 mData.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001519}
1520
1521void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1522{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001523 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001524 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001525 ASSERT(index < mData.mTransformFeedbackVaryingVars.size());
1526 const sh::Varying &varying = mData.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001527 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1528 if (length)
1529 {
1530 *length = lastNameIdx;
1531 }
1532 if (size)
1533 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001534 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001535 }
1536 if (type)
1537 {
1538 *type = varying.type;
1539 }
1540 if (name)
1541 {
1542 memcpy(name, varying.name.c_str(), lastNameIdx);
1543 name[lastNameIdx] = '\0';
1544 }
1545 }
1546}
1547
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001548GLsizei Program::getTransformFeedbackVaryingCount() const
1549{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001550 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001551 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001552 return static_cast<GLsizei>(mData.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001553 }
1554 else
1555 {
1556 return 0;
1557 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001558}
1559
1560GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1561{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001562 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001563 {
1564 GLsizei maxSize = 0;
Jamie Madillccdf74b2015-08-18 10:46:12 -04001565 for (const sh::Varying &varying : mData.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001566 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001567 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1568 }
1569
1570 return maxSize;
1571 }
1572 else
1573 {
1574 return 0;
1575 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001576}
1577
1578GLenum Program::getTransformFeedbackBufferMode() const
1579{
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001580 return mData.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001581}
1582
Jamie Madillada9ecc2015-08-17 12:53:37 -04001583// static
1584bool Program::linkVaryings(InfoLog &infoLog,
1585 const Shader *vertexShader,
1586 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001587{
Jamie Madill4cff2472015-08-21 16:53:18 -04001588 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1589 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001590
Jamie Madill4cff2472015-08-21 16:53:18 -04001591 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001592 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001593 bool matched = false;
1594
1595 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001596 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001597 {
1598 continue;
1599 }
1600
Jamie Madill4cff2472015-08-21 16:53:18 -04001601 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001602 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001603 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001604 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001605 ASSERT(!input.isBuiltIn());
1606 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001607 {
1608 return false;
1609 }
1610
Geoff Lang7dd2e102014-11-10 15:19:26 -05001611 matched = true;
1612 break;
1613 }
1614 }
1615
1616 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001617 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001618 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001619 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620 return false;
1621 }
1622 }
1623
Jamie Madillada9ecc2015-08-17 12:53:37 -04001624 // TODO(jmadill): verify no unmatched vertex varyings?
1625
Geoff Lang7dd2e102014-11-10 15:19:26 -05001626 return true;
1627}
1628
Jamie Madill62d31cb2015-09-11 13:25:51 -04001629bool Program::linkUniforms(gl::InfoLog &infoLog, const gl::Caps &caps)
Jamie Madillea918db2015-08-18 14:48:59 -04001630{
1631 const std::vector<sh::Uniform> &vertexUniforms = mData.mAttachedVertexShader->getUniforms();
1632 const std::vector<sh::Uniform> &fragmentUniforms = mData.mAttachedFragmentShader->getUniforms();
1633
1634 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001635 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001636
1637 for (const sh::Uniform &vertexUniform : vertexUniforms)
1638 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001639 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001640 }
1641
1642 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1643 {
1644 auto entry = linkedUniforms.find(fragmentUniform.name);
1645 if (entry != linkedUniforms.end())
1646 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001647 LinkedUniform *vertexUniform = &entry->second;
1648 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1649 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001650 {
1651 return false;
1652 }
1653 }
1654 }
1655
Jamie Madill62d31cb2015-09-11 13:25:51 -04001656 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1657 // Also check the maximum uniform vector and sampler counts.
1658 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1659 {
1660 return false;
1661 }
1662
1663 indexUniforms();
1664
Jamie Madillea918db2015-08-18 14:48:59 -04001665 return true;
1666}
1667
Jamie Madill62d31cb2015-09-11 13:25:51 -04001668void Program::indexUniforms()
1669{
1670 for (size_t uniformIndex = 0; uniformIndex < mData.mUniforms.size(); uniformIndex++)
1671 {
1672 const gl::LinkedUniform &uniform = mData.mUniforms[uniformIndex];
1673
1674 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1675 {
1676 if (!uniform.isBuiltIn())
1677 {
1678 // Assign in-order uniform locations
1679 mData.mUniformLocations.push_back(gl::VariableLocation(
1680 uniform.name, arrayIndex, static_cast<unsigned int>(uniformIndex)));
1681 }
1682 }
1683 }
1684}
1685
Geoff Lang7dd2e102014-11-10 15:19:26 -05001686bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1687{
Jamie Madillc4c744222015-11-04 09:39:47 -05001688 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1689 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001690 {
1691 return false;
1692 }
1693
1694 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1695 {
Jamie Madillf6113162015-05-07 11:49:21 -04001696 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001697 return false;
1698 }
1699
1700 return true;
1701}
1702
1703// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001704bool Program::linkAttributes(const gl::Data &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001705 InfoLog &infoLog,
1706 const AttributeBindings &attributeBindings,
1707 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001708{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001709 unsigned int usedLocations = 0;
Jamie Madillc349ec02015-08-21 16:53:12 -04001710 mData.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001711 GLuint maxAttribs = data.caps->maxVertexAttributes;
1712
1713 // TODO(jmadill): handle aliasing robustly
Jamie Madillc349ec02015-08-21 16:53:12 -04001714 if (mData.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001715 {
Jamie Madillf6113162015-05-07 11:49:21 -04001716 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001717 return false;
1718 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001719
Jamie Madillc349ec02015-08-21 16:53:12 -04001720 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001721
Jamie Madillc349ec02015-08-21 16:53:12 -04001722 // Link attributes that have a binding location
1723 for (sh::Attribute &attribute : mData.mAttributes)
1724 {
1725 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001726 ASSERT(attribute.staticUse);
1727
Jamie Madillc349ec02015-08-21 16:53:12 -04001728 int bindingLocation = attributeBindings.getAttributeBinding(attribute.name);
1729 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001730 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001731 attribute.location = bindingLocation;
1732 }
1733
1734 if (attribute.location != -1)
1735 {
1736 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001737 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001738
Jamie Madill63805b42015-08-25 13:17:39 -04001739 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001740 {
Jamie Madillf6113162015-05-07 11:49:21 -04001741 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001742 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001743
1744 return false;
1745 }
1746
Jamie Madill63805b42015-08-25 13:17:39 -04001747 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001748 {
Jamie Madill63805b42015-08-25 13:17:39 -04001749 const int regLocation = attribute.location + reg;
1750 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001751
1752 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001753 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001754 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001755 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001756 // TODO(jmadill): fix aliasing on ES2
1757 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001758 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001759 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001760 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001761 return false;
1762 }
1763 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001764 else
1765 {
Jamie Madill63805b42015-08-25 13:17:39 -04001766 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001767 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001768
Jamie Madill63805b42015-08-25 13:17:39 -04001769 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001770 }
1771 }
1772 }
1773
1774 // Link attributes that don't have a binding location
Jamie Madillc349ec02015-08-21 16:53:12 -04001775 for (sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001776 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001777 ASSERT(attribute.staticUse);
1778
Jamie Madillc349ec02015-08-21 16:53:12 -04001779 // Not set by glBindAttribLocation or by location layout qualifier
1780 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001781 {
Jamie Madill63805b42015-08-25 13:17:39 -04001782 int regs = VariableRegisterCount(attribute.type);
1783 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001784
Jamie Madill63805b42015-08-25 13:17:39 -04001785 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001786 {
Jamie Madillf6113162015-05-07 11:49:21 -04001787 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001788 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001789 }
1790
Jamie Madillc349ec02015-08-21 16:53:12 -04001791 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001792 }
1793 }
1794
Jamie Madillc349ec02015-08-21 16:53:12 -04001795 for (const sh::Attribute &attribute : mData.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001796 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001797 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001798 ASSERT(attribute.location != -1);
1799 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001800
Jamie Madill63805b42015-08-25 13:17:39 -04001801 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802 {
Jamie Madill63805b42015-08-25 13:17:39 -04001803 mData.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001804 }
1805 }
1806
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807 return true;
1808}
1809
Jamie Madille473dee2015-08-18 14:49:01 -04001810bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001811{
Jamie Madille473dee2015-08-18 14:49:01 -04001812 const Shader &vertexShader = *mData.mAttachedVertexShader;
1813 const Shader &fragmentShader = *mData.mAttachedFragmentShader;
1814
Geoff Lang7dd2e102014-11-10 15:19:26 -05001815 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1816 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001817
Geoff Lang7dd2e102014-11-10 15:19:26 -05001818 // Check that interface blocks defined in the vertex and fragment shaders are identical
1819 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1820 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001821
1822 GLuint vertexBlockCount = 0;
1823 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001824 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001825 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001826
1827 // Note: shared and std140 layouts are always considered active
1828 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1829 {
1830 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1831 {
1832 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1833 << caps.maxVertexUniformBlocks << ")";
1834 return false;
1835 }
1836 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837 }
Jamie Madille473dee2015-08-18 14:49:01 -04001838
1839 GLuint fragmentBlockCount = 0;
1840 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841 {
Jamie Madille473dee2015-08-18 14:49:01 -04001842 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001843 if (entry != linkedUniformBlocks.end())
1844 {
1845 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1846 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1847 {
1848 return false;
1849 }
1850 }
Jamie Madille473dee2015-08-18 14:49:01 -04001851
Geoff Lang7dd2e102014-11-10 15:19:26 -05001852 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001853 if (fragmentInterfaceBlock.staticUse ||
1854 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001855 {
Jamie Madille473dee2015-08-18 14:49:01 -04001856 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001857 {
Jamie Madille473dee2015-08-18 14:49:01 -04001858 infoLog
1859 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1860 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861 return false;
1862 }
1863 }
1864 }
Jamie Madille473dee2015-08-18 14:49:01 -04001865
Geoff Lang7dd2e102014-11-10 15:19:26 -05001866 return true;
1867}
1868
1869bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1870 const sh::InterfaceBlock &fragmentInterfaceBlock)
1871{
1872 const char* blockName = vertexInterfaceBlock.name.c_str();
1873 // validate blocks for the same member types
1874 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1875 {
Jamie Madillf6113162015-05-07 11:49:21 -04001876 infoLog << "Types for interface block '" << blockName
1877 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001878 return false;
1879 }
1880 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1881 {
Jamie Madillf6113162015-05-07 11:49:21 -04001882 infoLog << "Array sizes differ for interface block '" << blockName
1883 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884 return false;
1885 }
1886 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
1887 {
Jamie Madillf6113162015-05-07 11:49:21 -04001888 infoLog << "Layout qualifiers differ for interface block '" << blockName
1889 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001890 return false;
1891 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001892 const unsigned int numBlockMembers =
1893 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001894 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
1895 {
1896 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
1897 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
1898 if (vertexMember.name != fragmentMember.name)
1899 {
Jamie Madillf6113162015-05-07 11:49:21 -04001900 infoLog << "Name mismatch for field " << blockMemberIndex
1901 << " of interface block '" << blockName
1902 << "': (in vertex: '" << vertexMember.name
1903 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001904 return false;
1905 }
1906 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
1907 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
1908 {
1909 return false;
1910 }
1911 }
1912 return true;
1913}
1914
1915bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
1916 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
1917{
1918 if (vertexVariable.type != fragmentVariable.type)
1919 {
Jamie Madillf6113162015-05-07 11:49:21 -04001920 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001921 return false;
1922 }
1923 if (vertexVariable.arraySize != fragmentVariable.arraySize)
1924 {
Jamie Madillf6113162015-05-07 11:49:21 -04001925 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001926 return false;
1927 }
1928 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
1929 {
Jamie Madillf6113162015-05-07 11:49:21 -04001930 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001931 return false;
1932 }
1933
1934 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
1935 {
Jamie Madillf6113162015-05-07 11:49:21 -04001936 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001937 return false;
1938 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001939 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001940 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
1941 {
1942 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
1943 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
1944
1945 if (vertexMember.name != fragmentMember.name)
1946 {
Jamie Madillf6113162015-05-07 11:49:21 -04001947 infoLog << "Name mismatch for field '" << memberIndex
1948 << "' of " << variableName
1949 << ": (in vertex: '" << vertexMember.name
1950 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001951 return false;
1952 }
1953
1954 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
1955 vertexMember.name + "'";
1956
1957 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
1958 {
1959 return false;
1960 }
1961 }
1962
1963 return true;
1964}
1965
1966bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
1967{
Cooper Partin1acf4382015-06-12 12:38:57 -07001968#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
1969 const bool validatePrecision = true;
1970#else
1971 const bool validatePrecision = false;
1972#endif
1973
1974 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001975 {
1976 return false;
1977 }
1978
1979 return true;
1980}
1981
1982bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
1983{
1984 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
1985 {
1986 return false;
1987 }
1988
Jamie Madille9cc4692015-02-19 16:00:13 -05001989 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001990 {
Jamie Madillf6113162015-05-07 11:49:21 -04001991 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001992 return false;
1993 }
1994
1995 return true;
1996}
1997
Jamie Madillccdf74b2015-08-18 10:46:12 -04001998bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
1999 const std::vector<const sh::Varying *> &varyings,
2000 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001{
2002 size_t totalComponents = 0;
2003
Jamie Madillccdf74b2015-08-18 10:46:12 -04002004 std::set<std::string> uniqueNames;
2005
2006 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002007 {
2008 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002009 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002010 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002011 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002013 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002014 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002015 infoLog << "Two transform feedback varyings specify the same output variable ("
2016 << tfVaryingName << ").";
2017 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002018 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002019 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002020
Geoff Lang1a683462015-09-29 15:09:59 -04002021 if (varying->isArray())
2022 {
2023 infoLog << "Capture of arrays is undefined and not supported.";
2024 return false;
2025 }
2026
Jamie Madillccdf74b2015-08-18 10:46:12 -04002027 // TODO(jmadill): Investigate implementation limits on D3D11
2028 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madillada9ecc2015-08-17 12:53:37 -04002029 if (mData.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002030 componentCount > caps.maxTransformFeedbackSeparateComponents)
2031 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002032 infoLog << "Transform feedback varying's " << varying->name << " components ("
2033 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002034 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002035 return false;
2036 }
2037
2038 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002039 found = true;
2040 break;
2041 }
2042 }
2043
Jamie Madill89bb70e2015-08-31 14:18:39 -04002044 if (tfVaryingName.find('[') != std::string::npos)
2045 {
Geoff Lang1a683462015-09-29 15:09:59 -04002046 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002047 return false;
2048 }
2049
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2051 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002052 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 }
2054
Jamie Madillada9ecc2015-08-17 12:53:37 -04002055 if (mData.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002056 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057 {
Jamie Madillf6113162015-05-07 11:49:21 -04002058 infoLog << "Transform feedback varying total components (" << totalComponents
2059 << ") exceed the maximum interleaved components ("
2060 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002061 return false;
2062 }
2063
2064 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002065}
2066
Jamie Madillccdf74b2015-08-18 10:46:12 -04002067void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2068{
2069 // Gather the linked varyings that are used for transform feedback, they should all exist.
2070 mData.mTransformFeedbackVaryingVars.clear();
2071 for (const std::string &tfVaryingName : mData.mTransformFeedbackVaryingNames)
2072 {
2073 for (const sh::Varying *varying : varyings)
2074 {
2075 if (tfVaryingName == varying->name)
2076 {
2077 mData.mTransformFeedbackVaryingVars.push_back(*varying);
2078 break;
2079 }
2080 }
2081 }
2082}
2083
2084std::vector<const sh::Varying *> Program::getMergedVaryings() const
2085{
2086 std::set<std::string> uniqueNames;
2087 std::vector<const sh::Varying *> varyings;
2088
2089 for (const sh::Varying &varying : mData.mAttachedVertexShader->getVaryings())
2090 {
2091 if (uniqueNames.count(varying.name) == 0)
2092 {
2093 uniqueNames.insert(varying.name);
2094 varyings.push_back(&varying);
2095 }
2096 }
2097
2098 for (const sh::Varying &varying : mData.mAttachedFragmentShader->getVaryings())
2099 {
2100 if (uniqueNames.count(varying.name) == 0)
2101 {
2102 uniqueNames.insert(varying.name);
2103 varyings.push_back(&varying);
2104 }
2105 }
2106
2107 return varyings;
2108}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002109
2110void Program::linkOutputVariables()
2111{
2112 const Shader *fragmentShader = mData.mAttachedFragmentShader;
2113 ASSERT(fragmentShader != nullptr);
2114
2115 // Skip this step for GLES2 shaders.
2116 if (fragmentShader->getShaderVersion() == 100)
2117 return;
2118
Jamie Madilla0a9e122015-09-02 15:54:30 -04002119 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002120
2121 // TODO(jmadill): any caps validation here?
2122
2123 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2124 outputVariableIndex++)
2125 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002126 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002127
2128 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2129 if (outputVariable.isBuiltIn())
2130 continue;
2131
2132 // Since multiple output locations must be specified, use 0 for non-specified locations.
2133 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2134
2135 ASSERT(outputVariable.staticUse);
2136
2137 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2138 elementIndex++)
2139 {
2140 const int location = baseLocation + elementIndex;
2141 ASSERT(mData.mOutputVariables.count(location) == 0);
2142 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
2143 mData.mOutputVariables[location] =
2144 VariableLocation(outputVariable.name, element, outputVariableIndex);
2145 }
2146 }
2147}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002148
2149bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2150{
2151 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2152 VectorAndSamplerCount vsCounts;
2153
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002154 std::vector<LinkedUniform> samplerUniforms;
2155
Jamie Madill62d31cb2015-09-11 13:25:51 -04002156 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2157 {
2158 if (uniform.staticUse)
2159 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002160 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002161 }
2162 }
2163
2164 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2165 {
2166 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2167 << caps.maxVertexUniformVectors << ").";
2168 return false;
2169 }
2170
2171 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2172 {
2173 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2174 << caps.maxVertexTextureImageUnits << ").";
2175 return false;
2176 }
2177
2178 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2179 VectorAndSamplerCount fsCounts;
2180
2181 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2182 {
2183 if (uniform.staticUse)
2184 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002185 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002186 }
2187 }
2188
2189 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2190 {
2191 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2192 << caps.maxFragmentUniformVectors << ").";
2193 return false;
2194 }
2195
2196 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2197 {
2198 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2199 << caps.maxTextureImageUnits << ").";
2200 return false;
2201 }
2202
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002203 mSamplerUniformRange.start = static_cast<unsigned int>(mData.mUniforms.size());
2204 mSamplerUniformRange.end =
2205 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2206
2207 mData.mUniforms.insert(mData.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
2208
Jamie Madill62d31cb2015-09-11 13:25:51 -04002209 return true;
2210}
2211
2212Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002213 const std::string &fullName,
2214 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002215{
2216 VectorAndSamplerCount vectorAndSamplerCount;
2217
2218 if (uniform.isStruct())
2219 {
2220 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2221 {
2222 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2223
2224 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2225 {
2226 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2227 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2228
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002229 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002230 }
2231 }
2232
2233 return vectorAndSamplerCount;
2234 }
2235
2236 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002237 bool isSampler = IsSamplerType(uniform.type);
2238 if (!UniformInList(mData.getUniforms(), fullName) && !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002239 {
2240 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2241 uniform.arraySize, -1,
2242 sh::BlockMemberInfo::getDefaultBlockInfo());
2243 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002244
2245 // Store sampler uniforms separately, so we'll append them to the end of the list.
2246 if (isSampler)
2247 {
2248 samplerUniforms->push_back(linkedUniform);
2249 }
2250 else
2251 {
2252 mData.mUniforms.push_back(linkedUniform);
2253 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002254 }
2255
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002256 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002257
2258 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2259 // Likewise, don't count "real" uniforms towards sampler count.
2260 vectorAndSamplerCount.vectorCount =
2261 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002262 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002263
2264 return vectorAndSamplerCount;
2265}
2266
2267void Program::gatherInterfaceBlockInfo()
2268{
2269 std::set<std::string> visitedList;
2270
2271 const gl::Shader *vertexShader = mData.getAttachedVertexShader();
2272
2273 ASSERT(mData.mUniformBlocks.empty());
2274 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2275 {
2276 // Only 'packed' blocks are allowed to be considered inacive.
2277 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2278 continue;
2279
2280 if (visitedList.count(vertexBlock.name) > 0)
2281 continue;
2282
2283 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2284 visitedList.insert(vertexBlock.name);
2285 }
2286
2287 const gl::Shader *fragmentShader = mData.getAttachedFragmentShader();
2288
2289 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2290 {
2291 // Only 'packed' blocks are allowed to be considered inacive.
2292 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2293 continue;
2294
2295 if (visitedList.count(fragmentBlock.name) > 0)
2296 {
2297 for (gl::UniformBlock &block : mData.mUniformBlocks)
2298 {
2299 if (block.name == fragmentBlock.name)
2300 {
2301 block.fragmentStaticUse = fragmentBlock.staticUse;
2302 }
2303 }
2304
2305 continue;
2306 }
2307
2308 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2309 visitedList.insert(fragmentBlock.name);
2310 }
2311}
2312
Jamie Madill4a3c2342015-10-08 12:58:45 -04002313template <typename VarT>
2314void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2315 const std::string &prefix,
2316 int blockIndex)
2317{
2318 for (const VarT &field : fields)
2319 {
2320 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2321
2322 if (field.isStruct())
2323 {
2324 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2325 {
2326 const std::string uniformElementName =
2327 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2328 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2329 }
2330 }
2331 else
2332 {
2333 // If getBlockMemberInfo returns false, the uniform is optimized out.
2334 sh::BlockMemberInfo memberInfo;
2335 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2336 {
2337 continue;
2338 }
2339
2340 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2341 blockIndex, memberInfo);
2342
2343 // Since block uniforms have no location, we don't need to store them in the uniform
2344 // locations list.
2345 mData.mUniforms.push_back(newUniform);
2346 }
2347 }
2348}
2349
Jamie Madill62d31cb2015-09-11 13:25:51 -04002350void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2351{
Jamie Madill4a3c2342015-10-08 12:58:45 -04002352 int blockIndex = static_cast<int>(mData.mUniformBlocks.size());
2353 size_t blockSize = 0;
2354
2355 // Don't define this block at all if it's not active in the implementation.
2356 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2357 {
2358 return;
2359 }
2360
2361 // Track the first and last uniform index to determine the range of active uniforms in the
2362 // block.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002363 size_t firstBlockUniformIndex = mData.mUniforms.size();
Jamie Madill4a3c2342015-10-08 12:58:45 -04002364 defineUniformBlockMembers(interfaceBlock.fields, "", blockIndex);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002365 size_t lastBlockUniformIndex = mData.mUniforms.size();
2366
2367 std::vector<unsigned int> blockUniformIndexes;
2368 for (size_t blockUniformIndex = firstBlockUniformIndex;
2369 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2370 {
2371 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2372 }
2373
2374 if (interfaceBlock.arraySize > 0)
2375 {
2376 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2377 {
2378 UniformBlock block(interfaceBlock.name, true, arrayElement);
2379 block.memberUniformIndexes = blockUniformIndexes;
2380
2381 if (shaderType == GL_VERTEX_SHADER)
2382 {
2383 block.vertexStaticUse = interfaceBlock.staticUse;
2384 }
2385 else
2386 {
2387 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2388 block.fragmentStaticUse = interfaceBlock.staticUse;
2389 }
2390
Jamie Madill4a3c2342015-10-08 12:58:45 -04002391 // TODO(jmadill): Determine if we can ever have an inactive array element block.
2392 size_t blockElementSize = 0;
2393 if (!mProgram->getUniformBlockSize(block.nameWithArrayIndex(), &blockElementSize))
2394 {
2395 continue;
2396 }
2397
2398 ASSERT(blockElementSize == blockSize);
2399 block.dataSize = static_cast<unsigned int>(blockElementSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002400 mData.mUniformBlocks.push_back(block);
2401 }
2402 }
2403 else
2404 {
2405 UniformBlock block(interfaceBlock.name, false, 0);
2406 block.memberUniformIndexes = blockUniformIndexes;
2407
2408 if (shaderType == GL_VERTEX_SHADER)
2409 {
2410 block.vertexStaticUse = interfaceBlock.staticUse;
2411 }
2412 else
2413 {
2414 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2415 block.fragmentStaticUse = interfaceBlock.staticUse;
2416 }
2417
Jamie Madill4a3c2342015-10-08 12:58:45 -04002418 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002419 mData.mUniformBlocks.push_back(block);
2420 }
2421}
2422
2423template <typename T>
2424void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2425{
2426 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2427 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2428 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2429
2430 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2431 {
2432 // Do a cast conversion for boolean types. From the spec:
2433 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2434 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2435 for (GLsizei component = 0; component < count; ++component)
2436 {
2437 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2438 }
2439 }
2440 else
2441 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002442 // Invalide the validation cache if we modify the sampler data.
2443 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2444 {
2445 mCachedValidateSamplersResult.reset();
2446 }
2447
Jamie Madill62d31cb2015-09-11 13:25:51 -04002448 memcpy(destPointer, v, sizeof(T) * count);
2449 }
2450}
2451
2452template <size_t cols, size_t rows, typename T>
2453void Program::setMatrixUniformInternal(GLint location,
2454 GLsizei count,
2455 GLboolean transpose,
2456 const T *v)
2457{
2458 if (!transpose)
2459 {
2460 setUniformInternal(location, count * cols * rows, v);
2461 return;
2462 }
2463
2464 // Perform a transposing copy.
2465 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2466 LinkedUniform *linkedUniform = &mData.mUniforms[locationInfo.index];
2467 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2468 for (GLsizei element = 0; element < count; ++element)
2469 {
2470 size_t elementOffset = element * rows * cols;
2471
2472 for (size_t row = 0; row < rows; ++row)
2473 {
2474 for (size_t col = 0; col < cols; ++col)
2475 {
2476 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2477 }
2478 }
2479 }
2480}
2481
2482template <typename DestT>
2483void Program::getUniformInternal(GLint location, DestT *dataOut) const
2484{
2485 const VariableLocation &locationInfo = mData.mUniformLocations[location];
2486 const LinkedUniform &uniform = mData.mUniforms[locationInfo.index];
2487
2488 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2489
2490 GLenum componentType = VariableComponentType(uniform.type);
2491 if (componentType == GLTypeToGLenum<DestT>::value)
2492 {
2493 memcpy(dataOut, srcPointer, uniform.getElementSize());
2494 return;
2495 }
2496
2497 int components = VariableComponentCount(uniform.type) * uniform.elementCount();
2498
2499 switch (componentType)
2500 {
2501 case GL_INT:
2502 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2503 break;
2504 case GL_UNSIGNED_INT:
2505 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2506 break;
2507 case GL_BOOL:
2508 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2509 break;
2510 case GL_FLOAT:
2511 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2512 break;
2513 default:
2514 UNREACHABLE();
2515 }
2516}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517}