blob: b064abb4d9e69cc5be830497153b6998fabe705c [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"
Jamie Madill9082b982016-04-27 15:21:51 -040020#include "libANGLE/ContextState.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
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400141InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000142{
143}
144
145InfoLog::~InfoLog()
146{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000147}
148
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400149size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000150{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400151 const std::string &logString = mStream.str();
152 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000153}
154
Geoff Lange1a27752015-10-05 13:16:04 -0400155void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000156{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400157 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000158
159 if (bufSize > 0)
160 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400161 const std::string str(mStream.str());
162
163 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000164 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400165 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
166 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000167 }
168
169 infoLog[index] = '\0';
170 }
171
172 if (length)
173 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400174 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000175 }
176}
177
178// append a santized message to the program info log.
179// The D3D compiler includes a fake file path in some of the warning or error
180// messages, so lets remove all occurrences of this fake file path from the log.
181void InfoLog::appendSanitized(const char *message)
182{
183 std::string msg(message);
184
185 size_t found;
186 do
187 {
188 found = msg.find(g_fakepath);
189 if (found != std::string::npos)
190 {
191 msg.erase(found, strlen(g_fakepath));
192 }
193 }
194 while (found != std::string::npos);
195
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400196 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000197}
198
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000199void InfoLog::reset()
200{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000201}
202
Geoff Langd8605522016-04-13 10:19:12 -0400203VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000204{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500205}
206
Geoff Langd8605522016-04-13 10:19:12 -0400207VariableLocation::VariableLocation(const std::string &name,
208 unsigned int element,
209 unsigned int index)
210 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500211{
212}
213
Geoff Langd8605522016-04-13 10:19:12 -0400214void Program::Bindings::bindLocation(GLuint index, const std::string &name)
215{
216 mBindings[name] = index;
217}
218
219int Program::Bindings::getBinding(const std::string &name) const
220{
221 auto iter = mBindings.find(name);
222 return (iter != mBindings.end()) ? iter->second : -1;
223}
224
225Program::Bindings::const_iterator Program::Bindings::begin() const
226{
227 return mBindings.begin();
228}
229
230Program::Bindings::const_iterator Program::Bindings::end() const
231{
232 return mBindings.end();
233}
234
Jamie Madill48ef11b2016-04-27 15:21:52 -0400235ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500236 : mLabel(),
237 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400238 mAttachedVertexShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500239 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
240 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400241{
242}
243
Jamie Madill48ef11b2016-04-27 15:21:52 -0400244ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400245{
246 if (mAttachedVertexShader != nullptr)
247 {
248 mAttachedVertexShader->release();
249 }
250
251 if (mAttachedFragmentShader != nullptr)
252 {
253 mAttachedFragmentShader->release();
254 }
255}
256
Jamie Madill48ef11b2016-04-27 15:21:52 -0400257const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500258{
259 return mLabel;
260}
261
Jamie Madill48ef11b2016-04-27 15:21:52 -0400262const LinkedUniform *ProgramState::getUniformByName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400263{
264 for (const LinkedUniform &linkedUniform : mUniforms)
265 {
266 if (linkedUniform.name == name)
267 {
268 return &linkedUniform;
269 }
270 }
271
272 return nullptr;
273}
274
Jamie Madill48ef11b2016-04-27 15:21:52 -0400275GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400276{
277 size_t subscript = GL_INVALID_INDEX;
278 std::string baseName = gl::ParseUniformName(name, &subscript);
279
280 for (size_t location = 0; location < mUniformLocations.size(); ++location)
281 {
282 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400283 if (!uniformLocation.used)
284 {
285 continue;
286 }
287
288 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400289
290 if (uniform.name == baseName)
291 {
Geoff Langd8605522016-04-13 10:19:12 -0400292 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400293 {
Geoff Langd8605522016-04-13 10:19:12 -0400294 if (uniformLocation.element == subscript ||
295 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
296 {
297 return static_cast<GLint>(location);
298 }
299 }
300 else
301 {
302 if (subscript == GL_INVALID_INDEX)
303 {
304 return static_cast<GLint>(location);
305 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400306 }
307 }
308 }
309
310 return -1;
311}
312
Jamie Madill48ef11b2016-04-27 15:21:52 -0400313GLuint ProgramState::getUniformIndex(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400314{
315 size_t subscript = GL_INVALID_INDEX;
316 std::string baseName = gl::ParseUniformName(name, &subscript);
317
318 // The app is not allowed to specify array indices other than 0 for arrays of basic types
319 if (subscript != 0 && subscript != GL_INVALID_INDEX)
320 {
321 return GL_INVALID_INDEX;
322 }
323
324 for (size_t index = 0; index < mUniforms.size(); index++)
325 {
326 const LinkedUniform &uniform = mUniforms[index];
327 if (uniform.name == baseName)
328 {
329 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
330 {
331 return static_cast<GLuint>(index);
332 }
333 }
334 }
335
336 return GL_INVALID_INDEX;
337}
338
Jamie Madill7aea7e02016-05-10 10:39:45 -0400339Program::Program(rx::GLImplFactory *factory, ResourceManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400340 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400341 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500342 mLinked(false),
343 mDeleteStatus(false),
344 mRefCount(0),
345 mResourceManager(manager),
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400346 mHandle(handle),
347 mSamplerUniformRange(0, 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500348{
349 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000350
351 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500352 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353}
354
355Program::~Program()
356{
357 unlink(true);
daniel@transgaming.com71cd8682010-04-29 03:35:25 +0000358
Geoff Lang7dd2e102014-11-10 15:19:26 -0500359 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
Geoff Lang70d0f492015-12-10 17:45:46 -0500362void Program::setLabel(const std::string &label)
363{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400364 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500365}
366
367const std::string &Program::getLabel() const
368{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400369 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500370}
371
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372bool Program::attachShader(Shader *shader)
373{
374 if (shader->getType() == GL_VERTEX_SHADER)
375 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400376 if (mState.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377 {
378 return false;
379 }
380
Jamie Madill48ef11b2016-04-27 15:21:52 -0400381 mState.mAttachedVertexShader = shader;
382 mState.mAttachedVertexShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383 }
384 else if (shader->getType() == GL_FRAGMENT_SHADER)
385 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400386 if (mState.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387 {
388 return false;
389 }
390
Jamie Madill48ef11b2016-04-27 15:21:52 -0400391 mState.mAttachedFragmentShader = shader;
392 mState.mAttachedFragmentShader->addRef();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393 }
394 else UNREACHABLE();
395
396 return true;
397}
398
399bool Program::detachShader(Shader *shader)
400{
401 if (shader->getType() == GL_VERTEX_SHADER)
402 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400403 if (mState.mAttachedVertexShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404 {
405 return false;
406 }
407
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400408 shader->release();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400409 mState.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410 }
411 else if (shader->getType() == GL_FRAGMENT_SHADER)
412 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400413 if (mState.mAttachedFragmentShader != shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414 {
415 return false;
416 }
417
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400418 shader->release();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400419 mState.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420 }
421 else UNREACHABLE();
422
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423 return true;
424}
425
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000426int Program::getAttachedShadersCount() const
427{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400428 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000429}
430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431void Program::bindAttributeLocation(GLuint index, const char *name)
432{
Geoff Langd8605522016-04-13 10:19:12 -0400433 mAttributeBindings.bindLocation(index, name);
434}
435
436void Program::bindUniformLocation(GLuint index, const char *name)
437{
438 // Bind the base uniform name only since array indices other than 0 cannot be bound
439 mUniformBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440}
441
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442// Links the HLSL code of the vertex and pixel shader by matching up their varyings,
443// compiling them into binaries, determining the attribute mappings, and collecting
444// a list of uniforms
Jamie Madill9082b982016-04-27 15:21:51 -0400445Error Program::link(const ContextState &data)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000446{
447 unlink(false);
448
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000449 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000450 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000451
Jamie Madill48ef11b2016-04-27 15:21:52 -0400452 if (!mState.mAttachedFragmentShader || !mState.mAttachedFragmentShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500453 {
454 return Error(GL_NO_ERROR);
455 }
Jamie Madill48ef11b2016-04-27 15:21:52 -0400456 ASSERT(mState.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500457
Jamie Madill48ef11b2016-04-27 15:21:52 -0400458 if (!mState.mAttachedVertexShader || !mState.mAttachedVertexShader->isCompiled())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500459 {
460 return Error(GL_NO_ERROR);
461 }
Jamie Madill48ef11b2016-04-27 15:21:52 -0400462 ASSERT(mState.mAttachedVertexShader->getType() == GL_VERTEX_SHADER);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500463
Jamie Madill48ef11b2016-04-27 15:21:52 -0400464 if (!linkAttributes(data, mInfoLog, mAttributeBindings, mState.mAttachedVertexShader))
Jamie Madill437d2662014-12-05 14:23:35 -0500465 {
466 return Error(GL_NO_ERROR);
467 }
468
Jamie Madill48ef11b2016-04-27 15:21:52 -0400469 if (!linkVaryings(mInfoLog, mState.mAttachedVertexShader, mState.mAttachedFragmentShader))
Jamie Madillada9ecc2015-08-17 12:53:37 -0400470 {
471 return Error(GL_NO_ERROR);
472 }
473
Geoff Langd8605522016-04-13 10:19:12 -0400474 if (!linkUniforms(mInfoLog, *data.caps, mUniformBindings))
Jamie Madillea918db2015-08-18 14:48:59 -0400475 {
476 return Error(GL_NO_ERROR);
477 }
478
Jamie Madille473dee2015-08-18 14:49:01 -0400479 if (!linkUniformBlocks(mInfoLog, *data.caps))
480 {
481 return Error(GL_NO_ERROR);
482 }
483
Jamie Madillccdf74b2015-08-18 10:46:12 -0400484 const auto &mergedVaryings = getMergedVaryings();
485
486 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, *data.caps))
487 {
488 return Error(GL_NO_ERROR);
489 }
490
Jamie Madill80a6fc02015-08-21 16:53:16 -0400491 linkOutputVariables();
492
Jamie Madillf5f4ad22015-09-02 18:32:38 +0000493 rx::LinkResult result = mProgram->link(data, mInfoLog);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500494 if (result.error.isError() || !result.linkSuccess)
495 {
Geoff Lang7dd2e102014-11-10 15:19:26 -0500496 return result.error;
497 }
498
Jamie Madillccdf74b2015-08-18 10:46:12 -0400499 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill4a3c2342015-10-08 12:58:45 -0400500 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400501
Geoff Lang7dd2e102014-11-10 15:19:26 -0500502 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400503 return gl::Error(GL_NO_ERROR);
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000504}
505
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000506// Returns the program object to an unlinked state, before re-linking, or at destruction
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507void Program::unlink(bool destroy)
508{
509 if (destroy) // Object being destructed
510 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400511 if (mState.mAttachedFragmentShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400513 mState.mAttachedFragmentShader->release();
514 mState.mAttachedFragmentShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515 }
516
Jamie Madill48ef11b2016-04-27 15:21:52 -0400517 if (mState.mAttachedVertexShader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400519 mState.mAttachedVertexShader->release();
520 mState.mAttachedVertexShader = nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522 }
523
Jamie Madill48ef11b2016-04-27 15:21:52 -0400524 mState.mAttributes.clear();
525 mState.mActiveAttribLocationsMask.reset();
526 mState.mTransformFeedbackVaryingVars.clear();
527 mState.mUniforms.clear();
528 mState.mUniformLocations.clear();
529 mState.mUniformBlocks.clear();
530 mState.mOutputVariables.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500531
Geoff Lang7dd2e102014-11-10 15:19:26 -0500532 mValidated = false;
533
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000534 mLinked = false;
535}
536
Geoff Lange1a27752015-10-05 13:16:04 -0400537bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000538{
539 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540}
541
Geoff Lang7dd2e102014-11-10 15:19:26 -0500542Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000543{
544 unlink(false);
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000545
Geoff Lang7dd2e102014-11-10 15:19:26 -0500546#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
547 return Error(GL_NO_ERROR);
548#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400549 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
550 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000551 {
Jamie Madillf6113162015-05-07 11:49:21 -0400552 mInfoLog << "Invalid program binary format.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500553 return Error(GL_NO_ERROR);
554 }
555
Geoff Langc46cc2f2015-10-01 17:16:20 -0400556 BinaryInputStream stream(binary, length);
557
Geoff Lang7dd2e102014-11-10 15:19:26 -0500558 int majorVersion = stream.readInt<int>();
559 int minorVersion = stream.readInt<int>();
560 if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
561 {
Jamie Madillf6113162015-05-07 11:49:21 -0400562 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500563 return Error(GL_NO_ERROR);
564 }
565
566 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
567 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
568 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
569 {
Jamie Madillf6113162015-05-07 11:49:21 -0400570 mInfoLog << "Invalid program binary version.";
Geoff Lang7dd2e102014-11-10 15:19:26 -0500571 return Error(GL_NO_ERROR);
572 }
573
Jamie Madill63805b42015-08-25 13:17:39 -0400574 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
575 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400576 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500577
Jamie Madill3da79b72015-04-27 11:09:17 -0400578 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400579 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400580 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
581 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400582 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400583 LoadShaderVar(&stream, &attrib);
584 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400585 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400586 }
587
Jamie Madill62d31cb2015-09-11 13:25:51 -0400588 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400589 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400590 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
591 {
592 LinkedUniform uniform;
593 LoadShaderVar(&stream, &uniform);
594
595 uniform.blockIndex = stream.readInt<int>();
596 uniform.blockInfo.offset = stream.readInt<int>();
597 uniform.blockInfo.arrayStride = stream.readInt<int>();
598 uniform.blockInfo.matrixStride = stream.readInt<int>();
599 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
600
Jamie Madill48ef11b2016-04-27 15:21:52 -0400601 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400602 }
603
604 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400605 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400606 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
607 uniformIndexIndex++)
608 {
609 VariableLocation variable;
610 stream.readString(&variable.name);
611 stream.readInt(&variable.element);
612 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400613 stream.readBool(&variable.used);
614 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400615
Jamie Madill48ef11b2016-04-27 15:21:52 -0400616 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400617 }
618
619 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400620 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400621 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
622 ++uniformBlockIndex)
623 {
624 UniformBlock uniformBlock;
625 stream.readString(&uniformBlock.name);
626 stream.readBool(&uniformBlock.isArray);
627 stream.readInt(&uniformBlock.arrayElement);
628 stream.readInt(&uniformBlock.dataSize);
629 stream.readBool(&uniformBlock.vertexStaticUse);
630 stream.readBool(&uniformBlock.fragmentStaticUse);
631
632 unsigned int numMembers = stream.readInt<unsigned int>();
633 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
634 {
635 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
636 }
637
Jamie Madill48ef11b2016-04-27 15:21:52 -0400638 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400639 }
640
Brandon Jones1048ea72015-10-06 15:34:52 -0700641 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400642 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700643 for (unsigned int transformFeedbackVaryingIndex = 0;
644 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
645 ++transformFeedbackVaryingIndex)
646 {
647 sh::Varying varying;
648 stream.readInt(&varying.arraySize);
649 stream.readInt(&varying.type);
650 stream.readString(&varying.name);
651
Jamie Madill48ef11b2016-04-27 15:21:52 -0400652 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700653 }
654
Jamie Madill48ef11b2016-04-27 15:21:52 -0400655 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400656
Jamie Madill80a6fc02015-08-21 16:53:16 -0400657 unsigned int outputVarCount = stream.readInt<unsigned int>();
658 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
659 {
660 int locationIndex = stream.readInt<int>();
661 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400662 stream.readInt(&locationData.element);
663 stream.readInt(&locationData.index);
664 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400665 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400666 }
667
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400668 stream.readInt(&mSamplerUniformRange.start);
669 stream.readInt(&mSamplerUniformRange.end);
670
Geoff Lang7dd2e102014-11-10 15:19:26 -0500671 rx::LinkResult result = mProgram->load(mInfoLog, &stream);
672 if (result.error.isError() || !result.linkSuccess)
673 {
Geoff Langb543aff2014-09-30 14:52:54 -0400674 return result.error;
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000675 }
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000676
Geoff Lang7dd2e102014-11-10 15:19:26 -0500677 mLinked = true;
Geoff Langb543aff2014-09-30 14:52:54 -0400678 return Error(GL_NO_ERROR);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500679#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
680}
681
682Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const
683{
684 if (binaryFormat)
685 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400686 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500687 }
688
689 BinaryOutputStream stream;
690
Geoff Lang7dd2e102014-11-10 15:19:26 -0500691 stream.writeInt(ANGLE_MAJOR_VERSION);
692 stream.writeInt(ANGLE_MINOR_VERSION);
693 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
694
Jamie Madill48ef11b2016-04-27 15:21:52 -0400695 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500696
Jamie Madill48ef11b2016-04-27 15:21:52 -0400697 stream.writeInt(mState.mAttributes.size());
698 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400699 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400700 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400701 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400702 }
703
Jamie Madill48ef11b2016-04-27 15:21:52 -0400704 stream.writeInt(mState.mUniforms.size());
705 for (const gl::LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400706 {
707 WriteShaderVar(&stream, uniform);
708
709 // FIXME: referenced
710
711 stream.writeInt(uniform.blockIndex);
712 stream.writeInt(uniform.blockInfo.offset);
713 stream.writeInt(uniform.blockInfo.arrayStride);
714 stream.writeInt(uniform.blockInfo.matrixStride);
715 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
716 }
717
Jamie Madill48ef11b2016-04-27 15:21:52 -0400718 stream.writeInt(mState.mUniformLocations.size());
719 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400720 {
721 stream.writeString(variable.name);
722 stream.writeInt(variable.element);
723 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400724 stream.writeInt(variable.used);
725 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400726 }
727
Jamie Madill48ef11b2016-04-27 15:21:52 -0400728 stream.writeInt(mState.mUniformBlocks.size());
729 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400730 {
731 stream.writeString(uniformBlock.name);
732 stream.writeInt(uniformBlock.isArray);
733 stream.writeInt(uniformBlock.arrayElement);
734 stream.writeInt(uniformBlock.dataSize);
735
736 stream.writeInt(uniformBlock.vertexStaticUse);
737 stream.writeInt(uniformBlock.fragmentStaticUse);
738
739 stream.writeInt(uniformBlock.memberUniformIndexes.size());
740 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
741 {
742 stream.writeInt(memberUniformIndex);
743 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400744 }
745
Jamie Madill48ef11b2016-04-27 15:21:52 -0400746 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
747 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -0700748 {
749 stream.writeInt(varying.arraySize);
750 stream.writeInt(varying.type);
751 stream.writeString(varying.name);
752 }
753
Jamie Madill48ef11b2016-04-27 15:21:52 -0400754 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400755
Jamie Madill48ef11b2016-04-27 15:21:52 -0400756 stream.writeInt(mState.mOutputVariables.size());
757 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -0400758 {
759 stream.writeInt(outputPair.first);
760 stream.writeInt(outputPair.second.element);
761 stream.writeInt(outputPair.second.index);
762 stream.writeString(outputPair.second.name);
763 }
764
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400765 stream.writeInt(mSamplerUniformRange.start);
766 stream.writeInt(mSamplerUniformRange.end);
767
Geoff Lang7dd2e102014-11-10 15:19:26 -0500768 gl::Error error = mProgram->save(&stream);
769 if (error.isError())
770 {
771 return error;
772 }
773
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700774 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -0400775 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500776
777 if (streamLength > bufSize)
778 {
779 if (length)
780 {
781 *length = 0;
782 }
783
784 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
785 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
786 // sizes and then copy it.
787 return Error(GL_INVALID_OPERATION);
788 }
789
790 if (binary)
791 {
792 char *ptr = reinterpret_cast<char*>(binary);
793
Jamie Madill48ef11b2016-04-27 15:21:52 -0400794 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500795 ptr += streamLength;
796
797 ASSERT(ptr - streamLength == binary);
798 }
799
800 if (length)
801 {
802 *length = streamLength;
803 }
804
805 return Error(GL_NO_ERROR);
806}
807
808GLint Program::getBinaryLength() const
809{
810 GLint length;
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400811 Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500812 if (error.isError())
813 {
814 return 0;
815 }
816
817 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000818}
819
Geoff Langc5629752015-12-07 16:29:04 -0500820void Program::setBinaryRetrievableHint(bool retrievable)
821{
822 // TODO(jmadill) : replace with dirty bits
823 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400824 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -0500825}
826
827bool Program::getBinaryRetrievableHint() const
828{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400829 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -0500830}
831
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000832void Program::release()
833{
834 mRefCount--;
835
836 if (mRefCount == 0 && mDeleteStatus)
837 {
838 mResourceManager->deleteProgram(mHandle);
839 }
840}
841
842void Program::addRef()
843{
844 mRefCount++;
845}
846
847unsigned int Program::getRefCount() const
848{
849 return mRefCount;
850}
851
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000852int Program::getInfoLogLength() const
853{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400854 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000855}
856
Geoff Lange1a27752015-10-05 13:16:04 -0400857void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000858{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000859 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000860}
861
Geoff Lange1a27752015-10-05 13:16:04 -0400862void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000863{
864 int total = 0;
865
Jamie Madill48ef11b2016-04-27 15:21:52 -0400866 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000867 {
868 if (total < maxCount)
869 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400870 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200871 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000872 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000873 }
874
Jamie Madill48ef11b2016-04-27 15:21:52 -0400875 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000876 {
877 if (total < maxCount)
878 {
Jamie Madill48ef11b2016-04-27 15:21:52 -0400879 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +0200880 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000881 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000882 }
883
884 if (count)
885 {
886 *count = total;
887 }
888}
889
Geoff Lange1a27752015-10-05 13:16:04 -0400890GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500891{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400892 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500893 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400894 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500895 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400896 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500897 }
898 }
899
Austin Kinrossb8af7232015-03-16 22:33:25 -0700900 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500901}
902
Jamie Madill63805b42015-08-25 13:17:39 -0400903bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -0400904{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400905 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
906 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -0500907}
908
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000909void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
910{
Jamie Madillc349ec02015-08-21 16:53:12 -0400911 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000912 {
913 if (bufsize > 0)
914 {
915 name[0] = '\0';
916 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500917
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000918 if (length)
919 {
920 *length = 0;
921 }
922
923 *type = GL_NONE;
924 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -0400925 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000926 }
Jamie Madillc349ec02015-08-21 16:53:12 -0400927
928 size_t attributeIndex = 0;
929
Jamie Madill48ef11b2016-04-27 15:21:52 -0400930 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -0400931 {
932 // Skip over inactive attributes
933 if (attribute.staticUse)
934 {
935 if (static_cast<size_t>(index) == attributeIndex)
936 {
937 break;
938 }
939 attributeIndex++;
940 }
941 }
942
Jamie Madill48ef11b2016-04-27 15:21:52 -0400943 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
944 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -0400945
946 if (bufsize > 0)
947 {
948 const char *string = attrib.name.c_str();
949
950 strncpy(name, string, bufsize);
951 name[bufsize - 1] = '\0';
952
953 if (length)
954 {
955 *length = static_cast<GLsizei>(strlen(name));
956 }
957 }
958
959 // Always a single 'type' instance
960 *size = 1;
961 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000962}
963
Geoff Lange1a27752015-10-05 13:16:04 -0400964GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000965{
Jamie Madillc349ec02015-08-21 16:53:12 -0400966 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400967 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400968 return 0;
969 }
970
971 GLint count = 0;
972
Jamie Madill48ef11b2016-04-27 15:21:52 -0400973 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -0400974 {
975 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000976 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500977
978 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000979}
980
Geoff Lange1a27752015-10-05 13:16:04 -0400981GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000982{
Jamie Madillc349ec02015-08-21 16:53:12 -0400983 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -0400984 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400985 return 0;
986 }
987
988 size_t maxLength = 0;
989
Jamie Madill48ef11b2016-04-27 15:21:52 -0400990 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -0400991 {
992 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500993 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400994 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500995 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000996 }
Geoff Lang7dd2e102014-11-10 15:19:26 -0500997
Jamie Madillc349ec02015-08-21 16:53:12 -0400998 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500999}
1000
Geoff Lang7dd2e102014-11-10 15:19:26 -05001001GLint Program::getFragDataLocation(const std::string &name) const
1002{
1003 std::string baseName(name);
1004 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001005 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001006 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001007 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001008 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1009 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001010 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001011 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001012 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001013 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001014}
1015
Geoff Lange1a27752015-10-05 13:16:04 -04001016void Program::getActiveUniform(GLuint index,
1017 GLsizei bufsize,
1018 GLsizei *length,
1019 GLint *size,
1020 GLenum *type,
1021 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001022{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001023 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001024 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001025 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001026 ASSERT(index < mState.mUniforms.size());
1027 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001028
1029 if (bufsize > 0)
1030 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001031 std::string string = uniform.name;
1032 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001033 {
1034 string += "[0]";
1035 }
1036
1037 strncpy(name, string.c_str(), bufsize);
1038 name[bufsize - 1] = '\0';
1039
1040 if (length)
1041 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001042 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001043 }
1044 }
1045
Jamie Madill62d31cb2015-09-11 13:25:51 -04001046 *size = uniform.elementCount();
1047 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001048 }
1049 else
1050 {
1051 if (bufsize > 0)
1052 {
1053 name[0] = '\0';
1054 }
1055
1056 if (length)
1057 {
1058 *length = 0;
1059 }
1060
1061 *size = 0;
1062 *type = GL_NONE;
1063 }
1064}
1065
Geoff Lange1a27752015-10-05 13:16:04 -04001066GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001067{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001068 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001069 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001070 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001071 }
1072 else
1073 {
1074 return 0;
1075 }
1076}
1077
Geoff Lange1a27752015-10-05 13:16:04 -04001078GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001079{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001080 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001081
1082 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001083 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001084 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001085 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001086 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001087 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001088 size_t length = uniform.name.length() + 1u;
1089 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090 {
1091 length += 3; // Counting in "[0]".
1092 }
1093 maxLength = std::max(length, maxLength);
1094 }
1095 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001096 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001097
Jamie Madill62d31cb2015-09-11 13:25:51 -04001098 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001099}
1100
1101GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1102{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001103 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
1104 const gl::LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001105 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001106 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001107 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1108 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1109 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1110 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1111 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1112 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1113 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1114 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1115 default:
1116 UNREACHABLE();
1117 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001118 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001119 return 0;
1120}
1121
1122bool Program::isValidUniformLocation(GLint location) const
1123{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001124 ASSERT(rx::IsIntegerCastSafe<GLint>(mState.mUniformLocations.size()));
1125 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1126 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001127}
1128
1129bool Program::isIgnoredUniformLocation(GLint location) const
1130{
1131 // Location is ignored if it is -1 or it was bound but non-existant in the shader or optimized
1132 // out
1133 return location == -1 ||
Jamie Madill48ef11b2016-04-27 15:21:52 -04001134 (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1135 mState.mUniformLocations[static_cast<size_t>(location)].ignored);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001136}
1137
Jamie Madill62d31cb2015-09-11 13:25:51 -04001138const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001139{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001140 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1141 return mState.mUniforms[mState.mUniformLocations[location].index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142}
1143
Jamie Madill62d31cb2015-09-11 13:25:51 -04001144GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001145{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001146 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147}
1148
Jamie Madill62d31cb2015-09-11 13:25:51 -04001149GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001150{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001151 return mState.getUniformIndex(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001152}
1153
1154void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1155{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001156 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001157 mProgram->setUniform1fv(location, count, v);
1158}
1159
1160void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1161{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001162 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001163 mProgram->setUniform2fv(location, count, v);
1164}
1165
1166void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1167{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001168 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169 mProgram->setUniform3fv(location, count, v);
1170}
1171
1172void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1173{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001174 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001175 mProgram->setUniform4fv(location, count, v);
1176}
1177
1178void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1179{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001180 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001181 mProgram->setUniform1iv(location, count, v);
1182}
1183
1184void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1185{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001186 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001187 mProgram->setUniform2iv(location, count, v);
1188}
1189
1190void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1191{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001192 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001193 mProgram->setUniform3iv(location, count, v);
1194}
1195
1196void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1197{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001198 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001199 mProgram->setUniform4iv(location, count, v);
1200}
1201
1202void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1203{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001204 setUniformInternal(location, count * 1, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001205 mProgram->setUniform1uiv(location, count, v);
1206}
1207
1208void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1209{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001210 setUniformInternal(location, count * 2, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001211 mProgram->setUniform2uiv(location, count, v);
1212}
1213
1214void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1215{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001216 setUniformInternal(location, count * 3, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001217 mProgram->setUniform3uiv(location, count, v);
1218}
1219
1220void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1221{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001222 setUniformInternal(location, count * 4, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223 mProgram->setUniform4uiv(location, count, v);
1224}
1225
1226void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1227{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001228 setMatrixUniformInternal<2, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001229 mProgram->setUniformMatrix2fv(location, count, transpose, v);
1230}
1231
1232void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1233{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001234 setMatrixUniformInternal<3, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001235 mProgram->setUniformMatrix3fv(location, count, transpose, v);
1236}
1237
1238void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1239{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001240 setMatrixUniformInternal<4, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001241 mProgram->setUniformMatrix4fv(location, count, transpose, v);
1242}
1243
1244void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1245{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001246 setMatrixUniformInternal<2, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001247 mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
1248}
1249
1250void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1251{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001252 setMatrixUniformInternal<2, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001253 mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
1254}
1255
1256void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1257{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001258 setMatrixUniformInternal<3, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001259 mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
1260}
1261
1262void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1263{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001264 setMatrixUniformInternal<3, 4>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001265 mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
1266}
1267
1268void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1269{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001270 setMatrixUniformInternal<4, 2>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271 mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
1272}
1273
1274void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1275{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001276 setMatrixUniformInternal<4, 3>(location, count, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001277 mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
1278}
1279
Geoff Lange1a27752015-10-05 13:16:04 -04001280void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001281{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001282 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283}
1284
Geoff Lange1a27752015-10-05 13:16:04 -04001285void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001286{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001287 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001288}
1289
Geoff Lange1a27752015-10-05 13:16:04 -04001290void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001291{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001292 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001293}
1294
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001295void Program::flagForDeletion()
1296{
1297 mDeleteStatus = true;
1298}
1299
1300bool Program::isFlaggedForDeletion() const
1301{
1302 return mDeleteStatus;
1303}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001304
Brandon Jones43a53e22014-08-28 16:23:22 -07001305void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001306{
1307 mInfoLog.reset();
1308
Geoff Lang7dd2e102014-11-10 15:19:26 -05001309 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001310 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001311 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001312 }
1313 else
1314 {
Jamie Madillf6113162015-05-07 11:49:21 -04001315 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001316 }
1317}
1318
Geoff Lang7dd2e102014-11-10 15:19:26 -05001319bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1320{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001321 // Skip cache if we're using an infolog, so we get the full error.
1322 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1323 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1324 {
1325 return mCachedValidateSamplersResult.value();
1326 }
1327
1328 if (mTextureUnitTypesCache.empty())
1329 {
1330 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1331 }
1332 else
1333 {
1334 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1335 }
1336
1337 // if any two active samplers in a program are of different types, but refer to the same
1338 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1339 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
1340 for (unsigned int samplerIndex = mSamplerUniformRange.start;
1341 samplerIndex < mSamplerUniformRange.end; ++samplerIndex)
1342 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001343 const LinkedUniform &uniform = mState.mUniforms[samplerIndex];
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001344 ASSERT(uniform.isSampler());
1345
1346 if (!uniform.staticUse)
1347 continue;
1348
1349 const GLuint *dataPtr = reinterpret_cast<const GLuint *>(uniform.getDataPtrToElement(0));
1350 GLenum textureType = SamplerTypeToTextureType(uniform.type);
1351
1352 for (unsigned int arrayElement = 0; arrayElement < uniform.elementCount(); ++arrayElement)
1353 {
1354 GLuint textureUnit = dataPtr[arrayElement];
1355
1356 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1357 {
1358 if (infoLog)
1359 {
1360 (*infoLog) << "Sampler uniform (" << textureUnit
1361 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1362 << caps.maxCombinedTextureImageUnits << ")";
1363 }
1364
1365 mCachedValidateSamplersResult = false;
1366 return false;
1367 }
1368
1369 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1370 {
1371 if (textureType != mTextureUnitTypesCache[textureUnit])
1372 {
1373 if (infoLog)
1374 {
1375 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1376 "image unit ("
1377 << textureUnit << ").";
1378 }
1379
1380 mCachedValidateSamplersResult = false;
1381 return false;
1382 }
1383 }
1384 else
1385 {
1386 mTextureUnitTypesCache[textureUnit] = textureType;
1387 }
1388 }
1389 }
1390
1391 mCachedValidateSamplersResult = true;
1392 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001393}
1394
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001395bool Program::isValidated() const
1396{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397 return mValidated;
1398}
1399
Geoff Lange1a27752015-10-05 13:16:04 -04001400GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001401{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001402 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001403}
1404
1405void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1406{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001407 ASSERT(
1408 uniformBlockIndex <
1409 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001410
Jamie Madill48ef11b2016-04-27 15:21:52 -04001411 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001412
1413 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001414 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415 std::string string = uniformBlock.name;
1416
Jamie Madill62d31cb2015-09-11 13:25:51 -04001417 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001418 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001419 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001420 }
1421
1422 strncpy(uniformBlockName, string.c_str(), bufSize);
1423 uniformBlockName[bufSize - 1] = '\0';
1424
1425 if (length)
1426 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001427 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001428 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001429 }
1430}
1431
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001433{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001434 ASSERT(
1435 uniformBlockIndex <
1436 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001437
Jamie Madill48ef11b2016-04-27 15:21:52 -04001438 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439
1440 switch (pname)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001441 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001442 case GL_UNIFORM_BLOCK_DATA_SIZE:
1443 *params = static_cast<GLint>(uniformBlock.dataSize);
1444 break;
1445 case GL_UNIFORM_BLOCK_NAME_LENGTH:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001446 *params =
1447 static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArray ? 3 : 0));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448 break;
1449 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
1450 *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
1451 break;
1452 case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
1453 {
1454 for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
1455 {
1456 params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
1457 }
1458 }
1459 break;
1460 case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001461 *params = static_cast<GLint>(uniformBlock.vertexStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462 break;
1463 case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
Jamie Madill62d31cb2015-09-11 13:25:51 -04001464 *params = static_cast<GLint>(uniformBlock.fragmentStaticUse);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001465 break;
1466 default: UNREACHABLE();
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001467 }
1468}
1469
Geoff Lange1a27752015-10-05 13:16:04 -04001470GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001471{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001472 int maxLength = 0;
1473
1474 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001475 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001476 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001477 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1478 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001479 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480 if (!uniformBlock.name.empty())
1481 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001482 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001483
1484 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001485 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486
1487 maxLength = std::max(length + arrayLength, maxLength);
1488 }
1489 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001490 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001491
1492 return maxLength;
1493}
1494
Geoff Lange1a27752015-10-05 13:16:04 -04001495GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001496{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001497 size_t subscript = GL_INVALID_INDEX;
1498 std::string baseName = gl::ParseUniformName(name, &subscript);
1499
Jamie Madill48ef11b2016-04-27 15:21:52 -04001500 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001501 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1502 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001503 const gl::UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001504 if (uniformBlock.name == baseName)
1505 {
1506 const bool arrayElementZero =
1507 (subscript == GL_INVALID_INDEX &&
1508 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1509 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1510 {
1511 return blockIndex;
1512 }
1513 }
1514 }
1515
1516 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001517}
1518
Jamie Madill62d31cb2015-09-11 13:25:51 -04001519const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001520{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001521 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1522 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001523}
1524
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001525void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1526{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001527 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Geoff Lang5d124a62015-09-15 13:03:27 -04001528 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001529}
1530
1531GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1532{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001533 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001534}
1535
1536void Program::resetUniformBlockBindings()
1537{
1538 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1539 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001540 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001541 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001542 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001543}
1544
Geoff Lang48dcae72014-02-05 16:28:24 -05001545void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1546{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001547 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001548 for (GLsizei i = 0; i < count; i++)
1549 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001550 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001551 }
1552
Jamie Madill48ef11b2016-04-27 15:21:52 -04001553 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001554}
1555
1556void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1557{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001558 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001559 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001560 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1561 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001562 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1563 if (length)
1564 {
1565 *length = lastNameIdx;
1566 }
1567 if (size)
1568 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001569 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001570 }
1571 if (type)
1572 {
1573 *type = varying.type;
1574 }
1575 if (name)
1576 {
1577 memcpy(name, varying.name.c_str(), lastNameIdx);
1578 name[lastNameIdx] = '\0';
1579 }
1580 }
1581}
1582
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001583GLsizei Program::getTransformFeedbackVaryingCount() const
1584{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001585 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001586 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001587 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001588 }
1589 else
1590 {
1591 return 0;
1592 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001593}
1594
1595GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1596{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001597 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001598 {
1599 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001600 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001601 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001602 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1603 }
1604
1605 return maxSize;
1606 }
1607 else
1608 {
1609 return 0;
1610 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001611}
1612
1613GLenum Program::getTransformFeedbackBufferMode() const
1614{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001615 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001616}
1617
Jamie Madillada9ecc2015-08-17 12:53:37 -04001618// static
1619bool Program::linkVaryings(InfoLog &infoLog,
1620 const Shader *vertexShader,
1621 const Shader *fragmentShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001622{
Jamie Madill4cff2472015-08-21 16:53:18 -04001623 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1624 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625
Jamie Madill4cff2472015-08-21 16:53:18 -04001626 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001627 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628 bool matched = false;
1629
1630 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001631 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632 {
1633 continue;
1634 }
1635
Jamie Madill4cff2472015-08-21 16:53:18 -04001636 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001637 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001638 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001639 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001640 ASSERT(!input.isBuiltIn());
1641 if (!linkValidateVaryings(infoLog, output.name, input, output))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001642 {
1643 return false;
1644 }
1645
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646 matched = true;
1647 break;
1648 }
1649 }
1650
1651 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001652 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001653 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001654 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001655 return false;
1656 }
1657 }
1658
Jamie Madillada9ecc2015-08-17 12:53:37 -04001659 // TODO(jmadill): verify no unmatched vertex varyings?
1660
Geoff Lang7dd2e102014-11-10 15:19:26 -05001661 return true;
1662}
1663
Geoff Langd8605522016-04-13 10:19:12 -04001664bool Program::linkUniforms(gl::InfoLog &infoLog,
1665 const gl::Caps &caps,
1666 const Bindings &uniformBindings)
Jamie Madillea918db2015-08-18 14:48:59 -04001667{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001668 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1669 const std::vector<sh::Uniform> &fragmentUniforms =
1670 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001671
1672 // Check that uniforms defined in the vertex and fragment shaders are identical
Jamie Madill62d31cb2015-09-11 13:25:51 -04001673 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madillea918db2015-08-18 14:48:59 -04001674
1675 for (const sh::Uniform &vertexUniform : vertexUniforms)
1676 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001677 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001678 }
1679
1680 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1681 {
1682 auto entry = linkedUniforms.find(fragmentUniform.name);
1683 if (entry != linkedUniforms.end())
1684 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001685 LinkedUniform *vertexUniform = &entry->second;
1686 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1687 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001688 {
1689 return false;
1690 }
1691 }
1692 }
1693
Jamie Madill62d31cb2015-09-11 13:25:51 -04001694 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1695 // Also check the maximum uniform vector and sampler counts.
1696 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1697 {
1698 return false;
1699 }
1700
Geoff Langd8605522016-04-13 10:19:12 -04001701 if (!indexUniforms(infoLog, caps, uniformBindings))
1702 {
1703 return false;
1704 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001705
Jamie Madillea918db2015-08-18 14:48:59 -04001706 return true;
1707}
1708
Geoff Langd8605522016-04-13 10:19:12 -04001709bool Program::indexUniforms(gl::InfoLog &infoLog,
1710 const gl::Caps &caps,
1711 const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001712{
Geoff Langd8605522016-04-13 10:19:12 -04001713 // Uniforms awaiting a location
1714 std::vector<VariableLocation> unboundUniforms;
1715 std::map<GLuint, VariableLocation> boundUniforms;
1716 int maxUniformLocation = -1;
1717
1718 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001719 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001720 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001721 const gl::LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001722
Geoff Langd8605522016-04-13 10:19:12 -04001723 if (uniform.isBuiltIn())
1724 {
1725 continue;
1726 }
1727
1728 int bindingLocation = uniformBindings.getBinding(uniform.name);
1729
1730 // Verify that this location isn't bound twice
1731 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
1732 {
1733 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
1734 return false;
1735 }
1736
Jamie Madill62d31cb2015-09-11 13:25:51 -04001737 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
1738 {
Geoff Langd8605522016-04-13 10:19:12 -04001739 VariableLocation location(uniform.name, arrayIndex,
1740 static_cast<unsigned int>(uniformIndex));
1741
1742 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001743 {
Geoff Langd8605522016-04-13 10:19:12 -04001744 boundUniforms[bindingLocation] = location;
1745 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
1746 }
1747 else
1748 {
1749 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001750 }
1751 }
1752 }
Geoff Langd8605522016-04-13 10:19:12 -04001753
1754 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
1755 // not be assigned to those locations.
1756 std::set<GLuint> reservedLocations;
1757 for (const auto &binding : uniformBindings)
1758 {
1759 GLuint location = binding.second;
1760 if (boundUniforms.find(location) == boundUniforms.end())
1761 {
1762 reservedLocations.insert(location);
1763 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
1764 }
1765 }
1766
1767 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04001768 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04001769 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
1770 static_cast<size_t>(maxUniformLocation + 1)));
1771
1772 // Assign bound uniforms
1773 for (const auto &boundUniform : boundUniforms)
1774 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001775 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04001776 }
1777
1778 // Assign reserved uniforms
1779 for (const auto &reservedLocation : reservedLocations)
1780 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001781 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04001782 }
1783
1784 // Assign unbound uniforms
1785 size_t nextUniformLocation = 0;
1786 for (const auto &unboundUniform : unboundUniforms)
1787 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001788 while (mState.mUniformLocations[nextUniformLocation].used ||
1789 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04001790 {
1791 nextUniformLocation++;
1792 }
1793
Jamie Madill48ef11b2016-04-27 15:21:52 -04001794 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
1795 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04001796 nextUniformLocation++;
1797 }
1798
1799 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04001800}
1801
Geoff Lang7dd2e102014-11-10 15:19:26 -05001802bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
1803{
Jamie Madillc4c744222015-11-04 09:39:47 -05001804 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1805 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001806 {
1807 return false;
1808 }
1809
1810 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1811 {
Jamie Madillf6113162015-05-07 11:49:21 -04001812 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001813 return false;
1814 }
1815
1816 return true;
1817}
1818
1819// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
Jamie Madill9082b982016-04-27 15:21:51 -04001820bool Program::linkAttributes(const ContextState &data,
Jamie Madill3da79b72015-04-27 11:09:17 -04001821 InfoLog &infoLog,
Geoff Langd8605522016-04-13 10:19:12 -04001822 const Bindings &attributeBindings,
Jamie Madill3da79b72015-04-27 11:09:17 -04001823 const Shader *vertexShader)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001824{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001825 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001826 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madill3da79b72015-04-27 11:09:17 -04001827 GLuint maxAttribs = data.caps->maxVertexAttributes;
1828
1829 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001830 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001831 {
Jamie Madillf6113162015-05-07 11:49:21 -04001832 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001833 return false;
1834 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001835
Jamie Madillc349ec02015-08-21 16:53:12 -04001836 std::vector<sh::Attribute *> usedAttribMap(data.caps->maxVertexAttributes, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001837
Jamie Madillc349ec02015-08-21 16:53:12 -04001838 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001839 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001840 {
1841 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842 ASSERT(attribute.staticUse);
1843
Geoff Langd8605522016-04-13 10:19:12 -04001844 int bindingLocation = attributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001845 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001846 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001847 attribute.location = bindingLocation;
1848 }
1849
1850 if (attribute.location != -1)
1851 {
1852 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001853 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001854
Jamie Madill63805b42015-08-25 13:17:39 -04001855 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001856 {
Jamie Madillf6113162015-05-07 11:49:21 -04001857 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001858 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001859
1860 return false;
1861 }
1862
Jamie Madill63805b42015-08-25 13:17:39 -04001863 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001864 {
Jamie Madill63805b42015-08-25 13:17:39 -04001865 const int regLocation = attribute.location + reg;
1866 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001867
1868 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001869 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001870 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001871 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001872 // TODO(jmadill): fix aliasing on ES2
1873 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001875 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001876 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001877 return false;
1878 }
1879 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001880 else
1881 {
Jamie Madill63805b42015-08-25 13:17:39 -04001882 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04001883 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884
Jamie Madill63805b42015-08-25 13:17:39 -04001885 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001886 }
1887 }
1888 }
1889
1890 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001891 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001892 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893 ASSERT(attribute.staticUse);
1894
Jamie Madillc349ec02015-08-21 16:53:12 -04001895 // Not set by glBindAttribLocation or by location layout qualifier
1896 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001897 {
Jamie Madill63805b42015-08-25 13:17:39 -04001898 int regs = VariableRegisterCount(attribute.type);
1899 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001900
Jamie Madill63805b42015-08-25 13:17:39 -04001901 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001902 {
Jamie Madillf6113162015-05-07 11:49:21 -04001903 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04001904 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001905 }
1906
Jamie Madillc349ec02015-08-21 16:53:12 -04001907 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001908 }
1909 }
1910
Jamie Madill48ef11b2016-04-27 15:21:52 -04001911 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001912 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001913 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04001914 ASSERT(attribute.location != -1);
1915 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04001916
Jamie Madill63805b42015-08-25 13:17:39 -04001917 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001919 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001920 }
1921 }
1922
Geoff Lang7dd2e102014-11-10 15:19:26 -05001923 return true;
1924}
1925
Jamie Madille473dee2015-08-18 14:49:01 -04001926bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001927{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001928 const Shader &vertexShader = *mState.mAttachedVertexShader;
1929 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
Jamie Madille473dee2015-08-18 14:49:01 -04001930
Geoff Lang7dd2e102014-11-10 15:19:26 -05001931 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
1932 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
Jamie Madille473dee2015-08-18 14:49:01 -04001933
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934 // Check that interface blocks defined in the vertex and fragment shaders are identical
1935 typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
1936 UniformBlockMap linkedUniformBlocks;
Jamie Madille473dee2015-08-18 14:49:01 -04001937
1938 GLuint vertexBlockCount = 0;
1939 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001940 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001941 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
Jamie Madille473dee2015-08-18 14:49:01 -04001942
1943 // Note: shared and std140 layouts are always considered active
1944 if (vertexInterfaceBlock.staticUse || vertexInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
1945 {
1946 if (++vertexBlockCount > caps.maxVertexUniformBlocks)
1947 {
1948 infoLog << "Vertex shader uniform block count exceed GL_MAX_VERTEX_UNIFORM_BLOCKS ("
1949 << caps.maxVertexUniformBlocks << ")";
1950 return false;
1951 }
1952 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001953 }
Jamie Madille473dee2015-08-18 14:49:01 -04001954
1955 GLuint fragmentBlockCount = 0;
1956 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001957 {
Jamie Madille473dee2015-08-18 14:49:01 -04001958 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959 if (entry != linkedUniformBlocks.end())
1960 {
1961 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
1962 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
1963 {
1964 return false;
1965 }
1966 }
Jamie Madille473dee2015-08-18 14:49:01 -04001967
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968 // Note: shared and std140 layouts are always considered active
Jamie Madille473dee2015-08-18 14:49:01 -04001969 if (fragmentInterfaceBlock.staticUse ||
1970 fragmentInterfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001971 {
Jamie Madille473dee2015-08-18 14:49:01 -04001972 if (++fragmentBlockCount > caps.maxFragmentUniformBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001973 {
Jamie Madille473dee2015-08-18 14:49:01 -04001974 infoLog
1975 << "Fragment shader uniform block count exceed GL_MAX_FRAGMENT_UNIFORM_BLOCKS ("
1976 << caps.maxFragmentUniformBlocks << ")";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977 return false;
1978 }
1979 }
1980 }
Jamie Madille473dee2015-08-18 14:49:01 -04001981
Geoff Lang7dd2e102014-11-10 15:19:26 -05001982 return true;
1983}
1984
1985bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
1986 const sh::InterfaceBlock &fragmentInterfaceBlock)
1987{
1988 const char* blockName = vertexInterfaceBlock.name.c_str();
1989 // validate blocks for the same member types
1990 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
1991 {
Jamie Madillf6113162015-05-07 11:49:21 -04001992 infoLog << "Types for interface block '" << blockName
1993 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001994 return false;
1995 }
1996 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
1997 {
Jamie Madillf6113162015-05-07 11:49:21 -04001998 infoLog << "Array sizes differ for interface block '" << blockName
1999 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002000 return false;
2001 }
2002 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2003 {
Jamie Madillf6113162015-05-07 11:49:21 -04002004 infoLog << "Layout qualifiers differ for interface block '" << blockName
2005 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002006 return false;
2007 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002008 const unsigned int numBlockMembers =
2009 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002010 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2011 {
2012 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2013 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2014 if (vertexMember.name != fragmentMember.name)
2015 {
Jamie Madillf6113162015-05-07 11:49:21 -04002016 infoLog << "Name mismatch for field " << blockMemberIndex
2017 << " of interface block '" << blockName
2018 << "': (in vertex: '" << vertexMember.name
2019 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002020 return false;
2021 }
2022 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2023 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2024 {
2025 return false;
2026 }
2027 }
2028 return true;
2029}
2030
2031bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2032 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2033{
2034 if (vertexVariable.type != fragmentVariable.type)
2035 {
Jamie Madillf6113162015-05-07 11:49:21 -04002036 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002037 return false;
2038 }
2039 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2040 {
Jamie Madillf6113162015-05-07 11:49:21 -04002041 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042 return false;
2043 }
2044 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2045 {
Jamie Madillf6113162015-05-07 11:49:21 -04002046 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002047 return false;
2048 }
2049
2050 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2051 {
Jamie Madillf6113162015-05-07 11:49:21 -04002052 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 return false;
2054 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002055 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002056 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2057 {
2058 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2059 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2060
2061 if (vertexMember.name != fragmentMember.name)
2062 {
Jamie Madillf6113162015-05-07 11:49:21 -04002063 infoLog << "Name mismatch for field '" << memberIndex
2064 << "' of " << variableName
2065 << ": (in vertex: '" << vertexMember.name
2066 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002067 return false;
2068 }
2069
2070 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2071 vertexMember.name + "'";
2072
2073 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2074 {
2075 return false;
2076 }
2077 }
2078
2079 return true;
2080}
2081
2082bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2083{
Cooper Partin1acf4382015-06-12 12:38:57 -07002084#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2085 const bool validatePrecision = true;
2086#else
2087 const bool validatePrecision = false;
2088#endif
2089
2090 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002091 {
2092 return false;
2093 }
2094
2095 return true;
2096}
2097
2098bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
2099{
2100 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2101 {
2102 return false;
2103 }
2104
Jamie Madille9cc4692015-02-19 16:00:13 -05002105 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002106 {
Jamie Madillf6113162015-05-07 11:49:21 -04002107 infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002108 return false;
2109 }
2110
2111 return true;
2112}
2113
Jamie Madillccdf74b2015-08-18 10:46:12 -04002114bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
2115 const std::vector<const sh::Varying *> &varyings,
2116 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117{
2118 size_t totalComponents = 0;
2119
Jamie Madillccdf74b2015-08-18 10:46:12 -04002120 std::set<std::string> uniqueNames;
2121
Jamie Madill48ef11b2016-04-27 15:21:52 -04002122 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002123 {
2124 bool found = false;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002125 for (const sh::Varying *varying : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002126 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002127 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002128 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002129 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002131 infoLog << "Two transform feedback varyings specify the same output variable ("
2132 << tfVaryingName << ").";
2133 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002135 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002136
Geoff Lang1a683462015-09-29 15:09:59 -04002137 if (varying->isArray())
2138 {
2139 infoLog << "Capture of arrays is undefined and not supported.";
2140 return false;
2141 }
2142
Jamie Madillccdf74b2015-08-18 10:46:12 -04002143 // TODO(jmadill): Investigate implementation limits on D3D11
2144 size_t componentCount = gl::VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002145 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002146 componentCount > caps.maxTransformFeedbackSeparateComponents)
2147 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002148 infoLog << "Transform feedback varying's " << varying->name << " components ("
2149 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002150 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002151 return false;
2152 }
2153
2154 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155 found = true;
2156 break;
2157 }
2158 }
2159
Jamie Madill89bb70e2015-08-31 14:18:39 -04002160 if (tfVaryingName.find('[') != std::string::npos)
2161 {
Geoff Lang1a683462015-09-29 15:09:59 -04002162 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002163 return false;
2164 }
2165
Geoff Lang7dd2e102014-11-10 15:19:26 -05002166 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2167 ASSERT(found);
Corentin Wallez54c34e02015-07-02 15:06:55 -04002168 UNUSED_ASSERTION_VARIABLE(found);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169 }
2170
Jamie Madill48ef11b2016-04-27 15:21:52 -04002171 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002172 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173 {
Jamie Madillf6113162015-05-07 11:49:21 -04002174 infoLog << "Transform feedback varying total components (" << totalComponents
2175 << ") exceed the maximum interleaved components ("
2176 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002177 return false;
2178 }
2179
2180 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002181}
2182
Jamie Madillccdf74b2015-08-18 10:46:12 -04002183void Program::gatherTransformFeedbackVaryings(const std::vector<const sh::Varying *> &varyings)
2184{
2185 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002186 mState.mTransformFeedbackVaryingVars.clear();
2187 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002188 {
2189 for (const sh::Varying *varying : varyings)
2190 {
2191 if (tfVaryingName == varying->name)
2192 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002193 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002194 break;
2195 }
2196 }
2197 }
2198}
2199
2200std::vector<const sh::Varying *> Program::getMergedVaryings() const
2201{
2202 std::set<std::string> uniqueNames;
2203 std::vector<const sh::Varying *> varyings;
2204
Jamie Madill48ef11b2016-04-27 15:21:52 -04002205 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002206 {
2207 if (uniqueNames.count(varying.name) == 0)
2208 {
2209 uniqueNames.insert(varying.name);
2210 varyings.push_back(&varying);
2211 }
2212 }
2213
Jamie Madill48ef11b2016-04-27 15:21:52 -04002214 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002215 {
2216 if (uniqueNames.count(varying.name) == 0)
2217 {
2218 uniqueNames.insert(varying.name);
2219 varyings.push_back(&varying);
2220 }
2221 }
2222
2223 return varyings;
2224}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002225
2226void Program::linkOutputVariables()
2227{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002228 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002229 ASSERT(fragmentShader != nullptr);
2230
2231 // Skip this step for GLES2 shaders.
2232 if (fragmentShader->getShaderVersion() == 100)
2233 return;
2234
Jamie Madilla0a9e122015-09-02 15:54:30 -04002235 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002236
2237 // TODO(jmadill): any caps validation here?
2238
2239 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2240 outputVariableIndex++)
2241 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002242 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002243
2244 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2245 if (outputVariable.isBuiltIn())
2246 continue;
2247
2248 // Since multiple output locations must be specified, use 0 for non-specified locations.
2249 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2250
2251 ASSERT(outputVariable.staticUse);
2252
2253 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2254 elementIndex++)
2255 {
2256 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002257 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002258 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002259 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002260 VariableLocation(outputVariable.name, element, outputVariableIndex);
2261 }
2262 }
2263}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002264
2265bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2266{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002267 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002268 VectorAndSamplerCount vsCounts;
2269
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002270 std::vector<LinkedUniform> samplerUniforms;
2271
Jamie Madill62d31cb2015-09-11 13:25:51 -04002272 for (const sh::Uniform &uniform : vertexShader->getUniforms())
2273 {
2274 if (uniform.staticUse)
2275 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002276 vsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002277 }
2278 }
2279
2280 if (vsCounts.vectorCount > caps.maxVertexUniformVectors)
2281 {
2282 infoLog << "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS ("
2283 << caps.maxVertexUniformVectors << ").";
2284 return false;
2285 }
2286
2287 if (vsCounts.samplerCount > caps.maxVertexTextureImageUnits)
2288 {
2289 infoLog << "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS ("
2290 << caps.maxVertexTextureImageUnits << ").";
2291 return false;
2292 }
2293
Jamie Madill48ef11b2016-04-27 15:21:52 -04002294 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002295 VectorAndSamplerCount fsCounts;
2296
2297 for (const sh::Uniform &uniform : fragmentShader->getUniforms())
2298 {
2299 if (uniform.staticUse)
2300 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002301 fsCounts += flattenUniform(uniform, uniform.name, &samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002302 }
2303 }
2304
2305 if (fsCounts.vectorCount > caps.maxFragmentUniformVectors)
2306 {
2307 infoLog << "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS ("
2308 << caps.maxFragmentUniformVectors << ").";
2309 return false;
2310 }
2311
2312 if (fsCounts.samplerCount > caps.maxTextureImageUnits)
2313 {
2314 infoLog << "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS ("
2315 << caps.maxTextureImageUnits << ").";
2316 return false;
2317 }
2318
Jamie Madill48ef11b2016-04-27 15:21:52 -04002319 mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002320 mSamplerUniformRange.end =
2321 mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
2322
Jamie Madill48ef11b2016-04-27 15:21:52 -04002323 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002324
Jamie Madill62d31cb2015-09-11 13:25:51 -04002325 return true;
2326}
2327
2328Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002329 const std::string &fullName,
2330 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002331{
2332 VectorAndSamplerCount vectorAndSamplerCount;
2333
2334 if (uniform.isStruct())
2335 {
2336 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2337 {
2338 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2339
2340 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2341 {
2342 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2343 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2344
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002345 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002346 }
2347 }
2348
2349 return vectorAndSamplerCount;
2350 }
2351
2352 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002353 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002354 if (!UniformInList(mState.getUniforms(), fullName) &&
2355 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002356 {
2357 gl::LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName,
2358 uniform.arraySize, -1,
2359 sh::BlockMemberInfo::getDefaultBlockInfo());
2360 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002361
2362 // Store sampler uniforms separately, so we'll append them to the end of the list.
2363 if (isSampler)
2364 {
2365 samplerUniforms->push_back(linkedUniform);
2366 }
2367 else
2368 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002369 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002370 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002371 }
2372
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002373 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002374
2375 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2376 // Likewise, don't count "real" uniforms towards sampler count.
2377 vectorAndSamplerCount.vectorCount =
2378 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002379 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002380
2381 return vectorAndSamplerCount;
2382}
2383
2384void Program::gatherInterfaceBlockInfo()
2385{
2386 std::set<std::string> visitedList;
2387
Jamie Madill48ef11b2016-04-27 15:21:52 -04002388 const gl::Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002389
Jamie Madill48ef11b2016-04-27 15:21:52 -04002390 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -04002391 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2392 {
2393 // Only 'packed' blocks are allowed to be considered inacive.
2394 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2395 continue;
2396
2397 if (visitedList.count(vertexBlock.name) > 0)
2398 continue;
2399
2400 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2401 visitedList.insert(vertexBlock.name);
2402 }
2403
Jamie Madill48ef11b2016-04-27 15:21:52 -04002404 const gl::Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002405
2406 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2407 {
2408 // Only 'packed' blocks are allowed to be considered inacive.
2409 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2410 continue;
2411
2412 if (visitedList.count(fragmentBlock.name) > 0)
2413 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002414 for (gl::UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002415 {
2416 if (block.name == fragmentBlock.name)
2417 {
2418 block.fragmentStaticUse = fragmentBlock.staticUse;
2419 }
2420 }
2421
2422 continue;
2423 }
2424
2425 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2426 visitedList.insert(fragmentBlock.name);
2427 }
2428}
2429
Jamie Madill4a3c2342015-10-08 12:58:45 -04002430template <typename VarT>
2431void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2432 const std::string &prefix,
2433 int blockIndex)
2434{
2435 for (const VarT &field : fields)
2436 {
2437 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2438
2439 if (field.isStruct())
2440 {
2441 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2442 {
2443 const std::string uniformElementName =
2444 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2445 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2446 }
2447 }
2448 else
2449 {
2450 // If getBlockMemberInfo returns false, the uniform is optimized out.
2451 sh::BlockMemberInfo memberInfo;
2452 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2453 {
2454 continue;
2455 }
2456
2457 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2458 blockIndex, memberInfo);
2459
2460 // Since block uniforms have no location, we don't need to store them in the uniform
2461 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002462 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002463 }
2464 }
2465}
2466
Jamie Madill62d31cb2015-09-11 13:25:51 -04002467void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2468{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002469 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002470 size_t blockSize = 0;
2471
2472 // Don't define this block at all if it's not active in the implementation.
2473 if (!mProgram->getUniformBlockSize(interfaceBlock.name, &blockSize))
2474 {
2475 return;
2476 }
2477
2478 // Track the first and last uniform index to determine the range of active uniforms in the
2479 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002480 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002481 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002482 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002483
2484 std::vector<unsigned int> blockUniformIndexes;
2485 for (size_t blockUniformIndex = firstBlockUniformIndex;
2486 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2487 {
2488 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2489 }
2490
2491 if (interfaceBlock.arraySize > 0)
2492 {
2493 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2494 {
2495 UniformBlock block(interfaceBlock.name, true, arrayElement);
2496 block.memberUniformIndexes = blockUniformIndexes;
2497
2498 if (shaderType == GL_VERTEX_SHADER)
2499 {
2500 block.vertexStaticUse = interfaceBlock.staticUse;
2501 }
2502 else
2503 {
2504 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2505 block.fragmentStaticUse = interfaceBlock.staticUse;
2506 }
2507
Jamie Madill4a3c2342015-10-08 12:58:45 -04002508 // TODO(jmadill): Determine if we can ever have an inactive array element block.
2509 size_t blockElementSize = 0;
2510 if (!mProgram->getUniformBlockSize(block.nameWithArrayIndex(), &blockElementSize))
2511 {
2512 continue;
2513 }
2514
2515 ASSERT(blockElementSize == blockSize);
2516 block.dataSize = static_cast<unsigned int>(blockElementSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002517 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002518 }
2519 }
2520 else
2521 {
2522 UniformBlock block(interfaceBlock.name, false, 0);
2523 block.memberUniformIndexes = blockUniformIndexes;
2524
2525 if (shaderType == GL_VERTEX_SHADER)
2526 {
2527 block.vertexStaticUse = interfaceBlock.staticUse;
2528 }
2529 else
2530 {
2531 ASSERT(shaderType == GL_FRAGMENT_SHADER);
2532 block.fragmentStaticUse = interfaceBlock.staticUse;
2533 }
2534
Jamie Madill4a3c2342015-10-08 12:58:45 -04002535 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002536 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002537 }
2538}
2539
2540template <typename T>
2541void Program::setUniformInternal(GLint location, GLsizei count, const T *v)
2542{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002543 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2544 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002545 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2546
2547 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2548 {
2549 // Do a cast conversion for boolean types. From the spec:
2550 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2551 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
2552 for (GLsizei component = 0; component < count; ++component)
2553 {
2554 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2555 }
2556 }
2557 else
2558 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002559 // Invalide the validation cache if we modify the sampler data.
2560 if (linkedUniform->isSampler() && memcmp(destPointer, v, sizeof(T) * count) != 0)
2561 {
2562 mCachedValidateSamplersResult.reset();
2563 }
2564
Jamie Madill62d31cb2015-09-11 13:25:51 -04002565 memcpy(destPointer, v, sizeof(T) * count);
2566 }
2567}
2568
2569template <size_t cols, size_t rows, typename T>
2570void Program::setMatrixUniformInternal(GLint location,
2571 GLsizei count,
2572 GLboolean transpose,
2573 const T *v)
2574{
2575 if (!transpose)
2576 {
2577 setUniformInternal(location, count * cols * rows, v);
2578 return;
2579 }
2580
2581 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002582 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2583 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002584 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
2585 for (GLsizei element = 0; element < count; ++element)
2586 {
2587 size_t elementOffset = element * rows * cols;
2588
2589 for (size_t row = 0; row < rows; ++row)
2590 {
2591 for (size_t col = 0; col < cols; ++col)
2592 {
2593 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2594 }
2595 }
2596 }
2597}
2598
2599template <typename DestT>
2600void Program::getUniformInternal(GLint location, DestT *dataOut) const
2601{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002602 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2603 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002604
2605 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2606
2607 GLenum componentType = VariableComponentType(uniform.type);
2608 if (componentType == GLTypeToGLenum<DestT>::value)
2609 {
2610 memcpy(dataOut, srcPointer, uniform.getElementSize());
2611 return;
2612 }
2613
Corentin Wallez6596c462016-03-17 17:26:58 -04002614 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002615
2616 switch (componentType)
2617 {
2618 case GL_INT:
2619 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2620 break;
2621 case GL_UNSIGNED_INT:
2622 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2623 break;
2624 case GL_BOOL:
2625 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2626 break;
2627 case GL_FLOAT:
2628 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2629 break;
2630 default:
2631 UNREACHABLE();
2632 }
2633}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634}