blob: 865b23a1bbc96f06bd435080b496dde76c2848ca [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 Madilla2c74982016-12-12 11:20:42 -050020#include "libANGLE/Context.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"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040023#include "libANGLE/renderer/GLImplFactory.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050024#include "libANGLE/renderer/ProgramImpl.h"
Jamie Madill192745a2016-12-22 15:58:21 -050025#include "libANGLE/VaryingPacking.h"
Jamie Madill62d31cb2015-09-11 13:25:51 -040026#include "libANGLE/queryconversions.h"
Jamie Madill53ea9cc2016-05-17 10:12:52 -040027#include "libANGLE/Uniform.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050028
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029namespace gl
30{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000031
Geoff Lang7dd2e102014-11-10 15:19:26 -050032namespace
33{
34
Jamie Madill62d31cb2015-09-11 13:25:51 -040035void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
36{
37 stream->writeInt(var.type);
38 stream->writeInt(var.precision);
39 stream->writeString(var.name);
40 stream->writeString(var.mappedName);
41 stream->writeInt(var.arraySize);
42 stream->writeInt(var.staticUse);
43 stream->writeString(var.structName);
44 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050045}
46
Jamie Madill62d31cb2015-09-11 13:25:51 -040047void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
48{
49 var->type = stream->readInt<GLenum>();
50 var->precision = stream->readInt<GLenum>();
51 var->name = stream->readString();
52 var->mappedName = stream->readString();
53 var->arraySize = stream->readInt<unsigned int>();
54 var->staticUse = stream->readBool();
55 var->structName = stream->readString();
56}
57
Jamie Madill62d31cb2015-09-11 13:25:51 -040058// This simplified cast function doesn't need to worry about advanced concepts like
59// depth range values, or casting to bool.
60template <typename DestT, typename SrcT>
61DestT UniformStateQueryCast(SrcT value);
62
63// From-Float-To-Integer Casts
64template <>
65GLint UniformStateQueryCast(GLfloat value)
66{
67 return clampCast<GLint>(roundf(value));
68}
69
70template <>
71GLuint UniformStateQueryCast(GLfloat value)
72{
73 return clampCast<GLuint>(roundf(value));
74}
75
76// From-Integer-to-Integer Casts
77template <>
78GLint UniformStateQueryCast(GLuint value)
79{
80 return clampCast<GLint>(value);
81}
82
83template <>
84GLuint UniformStateQueryCast(GLint value)
85{
86 return clampCast<GLuint>(value);
87}
88
89// From-Boolean-to-Anything Casts
90template <>
91GLfloat UniformStateQueryCast(GLboolean value)
92{
93 return (value == GL_TRUE ? 1.0f : 0.0f);
94}
95
96template <>
97GLint UniformStateQueryCast(GLboolean value)
98{
99 return (value == GL_TRUE ? 1 : 0);
100}
101
102template <>
103GLuint UniformStateQueryCast(GLboolean value)
104{
105 return (value == GL_TRUE ? 1u : 0u);
106}
107
108// Default to static_cast
109template <typename DestT, typename SrcT>
110DestT UniformStateQueryCast(SrcT value)
111{
112 return static_cast<DestT>(value);
113}
114
115template <typename SrcT, typename DestT>
116void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
117{
118 for (int comp = 0; comp < components; ++comp)
119 {
120 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
121 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
122 size_t offset = comp * 4;
123 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
124 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
125 }
126}
127
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400128bool UniformInList(const std::vector<LinkedUniform> &list, const std::string &name)
129{
130 for (const LinkedUniform &uniform : list)
131 {
132 if (uniform.name == name)
133 return true;
134 }
135
136 return false;
137}
138
Jamie Madill192745a2016-12-22 15:58:21 -0500139// true if varying x has a higher priority in packing than y
140bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
141{
142 return gl::CompareShaderVar(*x.varying, *y.varying);
143}
144
Jamie Madill62d31cb2015-09-11 13:25:51 -0400145} // anonymous namespace
146
Jamie Madill4a3c2342015-10-08 12:58:45 -0400147const char *const g_fakepath = "C:\\fakepath";
148
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400149InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000150{
151}
152
153InfoLog::~InfoLog()
154{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000155}
156
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400157size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000158{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400159 const std::string &logString = mStream.str();
160 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000161}
162
Geoff Lange1a27752015-10-05 13:16:04 -0400163void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000164{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400165 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000166
167 if (bufSize > 0)
168 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400169 const std::string str(mStream.str());
170
171 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000172 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400173 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
174 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000175 }
176
177 infoLog[index] = '\0';
178 }
179
180 if (length)
181 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400182 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000183 }
184}
185
186// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300187// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000188// messages, so lets remove all occurrences of this fake file path from the log.
189void InfoLog::appendSanitized(const char *message)
190{
191 std::string msg(message);
192
193 size_t found;
194 do
195 {
196 found = msg.find(g_fakepath);
197 if (found != std::string::npos)
198 {
199 msg.erase(found, strlen(g_fakepath));
200 }
201 }
202 while (found != std::string::npos);
203
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400204 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000205}
206
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000207void InfoLog::reset()
208{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000209}
210
Geoff Langd8605522016-04-13 10:19:12 -0400211VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000212{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500213}
214
Geoff Langd8605522016-04-13 10:19:12 -0400215VariableLocation::VariableLocation(const std::string &name,
216 unsigned int element,
217 unsigned int index)
218 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500219{
220}
221
Geoff Langd8605522016-04-13 10:19:12 -0400222void Program::Bindings::bindLocation(GLuint index, const std::string &name)
223{
224 mBindings[name] = index;
225}
226
227int Program::Bindings::getBinding(const std::string &name) const
228{
229 auto iter = mBindings.find(name);
230 return (iter != mBindings.end()) ? iter->second : -1;
231}
232
233Program::Bindings::const_iterator Program::Bindings::begin() const
234{
235 return mBindings.begin();
236}
237
238Program::Bindings::const_iterator Program::Bindings::end() const
239{
240 return mBindings.end();
241}
242
Jamie Madill48ef11b2016-04-27 15:21:52 -0400243ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500244 : mLabel(),
245 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400246 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300247 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500248 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500249 mSamplerUniformRange(0, 0),
Geoff Langc5629752015-12-07 16:29:04 -0500250 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400251{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300252 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400253}
254
Jamie Madill48ef11b2016-04-27 15:21:52 -0400255ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400256{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500257 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400258}
259
Jamie Madill48ef11b2016-04-27 15:21:52 -0400260const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500261{
262 return mLabel;
263}
264
Jamie Madill48ef11b2016-04-27 15:21:52 -0400265const LinkedUniform *ProgramState::getUniformByName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400266{
267 for (const LinkedUniform &linkedUniform : mUniforms)
268 {
269 if (linkedUniform.name == name)
270 {
271 return &linkedUniform;
272 }
273 }
274
275 return nullptr;
276}
277
Jamie Madill48ef11b2016-04-27 15:21:52 -0400278GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400279{
280 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -0500281 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400282
283 for (size_t location = 0; location < mUniformLocations.size(); ++location)
284 {
285 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400286 if (!uniformLocation.used)
287 {
288 continue;
289 }
290
291 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400292
293 if (uniform.name == baseName)
294 {
Geoff Langd8605522016-04-13 10:19:12 -0400295 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400296 {
Geoff Langd8605522016-04-13 10:19:12 -0400297 if (uniformLocation.element == subscript ||
298 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
299 {
300 return static_cast<GLint>(location);
301 }
302 }
303 else
304 {
305 if (subscript == GL_INVALID_INDEX)
306 {
307 return static_cast<GLint>(location);
308 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400309 }
310 }
311 }
312
313 return -1;
314}
315
Jamie Madille7d84322017-01-10 18:21:59 -0500316GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400317{
318 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -0500319 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400320
321 // The app is not allowed to specify array indices other than 0 for arrays of basic types
322 if (subscript != 0 && subscript != GL_INVALID_INDEX)
323 {
324 return GL_INVALID_INDEX;
325 }
326
327 for (size_t index = 0; index < mUniforms.size(); index++)
328 {
329 const LinkedUniform &uniform = mUniforms[index];
330 if (uniform.name == baseName)
331 {
332 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
333 {
334 return static_cast<GLuint>(index);
335 }
336 }
337 }
338
339 return GL_INVALID_INDEX;
340}
341
Jamie Madille7d84322017-01-10 18:21:59 -0500342GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
343{
344 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
345 return mUniformLocations[location].index;
346}
347
348Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
349{
350 GLuint index = getUniformIndexFromLocation(location);
351 if (!isSamplerUniformIndex(index))
352 {
353 return Optional<GLuint>::Invalid();
354 }
355
356 return getSamplerIndexFromUniformIndex(index);
357}
358
359bool ProgramState::isSamplerUniformIndex(GLuint index) const
360{
361 return index >= mSamplerUniformRange.start && index < mSamplerUniformRange.end;
362}
363
364GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
365{
366 ASSERT(isSamplerUniformIndex(uniformIndex));
367 return uniformIndex - mSamplerUniformRange.start;
368}
369
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500370Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400371 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400372 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500373 mLinked(false),
374 mDeleteStatus(false),
375 mRefCount(0),
376 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500377 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500378{
379 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000380
381 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500382 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383}
384
385Program::~Program()
386{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500387 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
388 !mState.mAttachedComputeShader);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500389 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390}
391
Jamie Madill6c1f6712017-02-14 19:08:04 -0500392void Program::destroy(const Context *context)
393{
394 if (mState.mAttachedVertexShader != nullptr)
395 {
396 mState.mAttachedVertexShader->release(context);
397 mState.mAttachedVertexShader = nullptr;
398 }
399
400 if (mState.mAttachedFragmentShader != nullptr)
401 {
402 mState.mAttachedFragmentShader->release(context);
403 mState.mAttachedFragmentShader = nullptr;
404 }
405
406 if (mState.mAttachedComputeShader != nullptr)
407 {
408 mState.mAttachedComputeShader->release(context);
409 mState.mAttachedComputeShader = nullptr;
410 }
411
412 mProgram->destroy(rx::SafeGetImpl(context));
413}
414
Geoff Lang70d0f492015-12-10 17:45:46 -0500415void Program::setLabel(const std::string &label)
416{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400417 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500418}
419
420const std::string &Program::getLabel() const
421{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400422 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500423}
424
Jamie Madillef300b12016-10-07 15:12:09 -0400425void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300427 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300429 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430 {
Jamie Madillef300b12016-10-07 15:12:09 -0400431 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300432 mState.mAttachedVertexShader = shader;
433 mState.mAttachedVertexShader->addRef();
434 break;
435 }
436 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437 {
Jamie Madillef300b12016-10-07 15:12:09 -0400438 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300439 mState.mAttachedFragmentShader = shader;
440 mState.mAttachedFragmentShader->addRef();
441 break;
442 }
443 case GL_COMPUTE_SHADER:
444 {
Jamie Madillef300b12016-10-07 15:12:09 -0400445 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300446 mState.mAttachedComputeShader = shader;
447 mState.mAttachedComputeShader->addRef();
448 break;
449 }
450 default:
451 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453}
454
Jamie Madill6c1f6712017-02-14 19:08:04 -0500455bool Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300457 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300459 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300461 if (mState.mAttachedVertexShader != shader)
462 {
463 return false;
464 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465
Jamie Madill6c1f6712017-02-14 19:08:04 -0500466 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300467 mState.mAttachedVertexShader = nullptr;
468 break;
469 }
470 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300472 if (mState.mAttachedFragmentShader != shader)
473 {
474 return false;
475 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000476
Jamie Madill6c1f6712017-02-14 19:08:04 -0500477 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300478 mState.mAttachedFragmentShader = nullptr;
479 break;
480 }
481 case GL_COMPUTE_SHADER:
482 {
483 if (mState.mAttachedComputeShader != shader)
484 {
485 return false;
486 }
487
Jamie Madill6c1f6712017-02-14 19:08:04 -0500488 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300489 mState.mAttachedComputeShader = nullptr;
490 break;
491 }
492 default:
493 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496 return true;
497}
498
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000499int Program::getAttachedShadersCount() const
500{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300501 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
502 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000503}
504
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505void Program::bindAttributeLocation(GLuint index, const char *name)
506{
Geoff Langd8605522016-04-13 10:19:12 -0400507 mAttributeBindings.bindLocation(index, name);
508}
509
510void Program::bindUniformLocation(GLuint index, const char *name)
511{
512 // Bind the base uniform name only since array indices other than 0 cannot be bound
513 mUniformBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514}
515
Sami Väisänen46eaa942016-06-29 10:26:37 +0300516void Program::bindFragmentInputLocation(GLint index, const char *name)
517{
518 mFragmentInputBindings.bindLocation(index, name);
519}
520
521BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
522{
523 BindingInfo ret;
524 ret.type = GL_NONE;
525 ret.valid = false;
526
527 const Shader *fragmentShader = mState.getAttachedFragmentShader();
528 ASSERT(fragmentShader);
529
530 // Find the actual fragment shader varying we're interested in
531 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
532
533 for (const auto &binding : mFragmentInputBindings)
534 {
535 if (binding.second != static_cast<GLuint>(index))
536 continue;
537
538 ret.valid = true;
539
540 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400541 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300542
543 for (const auto &in : inputs)
544 {
545 if (in.name == originalName)
546 {
547 if (in.isArray())
548 {
549 // The client wants to bind either "name" or "name[0]".
550 // GL ES 3.1 spec refers to active array names with language such as:
551 // "if the string identifies the base name of an active array, where the
552 // string would exactly match the name of the variable if the suffix "[0]"
553 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400554 if (arrayIndex == GL_INVALID_INDEX)
555 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300556
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400557 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300558 }
559 else
560 {
561 ret.name = in.mappedName;
562 }
563 ret.type = in.type;
564 return ret;
565 }
566 }
567 }
568
569 return ret;
570}
571
572void Program::pathFragmentInputGen(GLint index,
573 GLenum genMode,
574 GLint components,
575 const GLfloat *coeffs)
576{
577 // If the location is -1 then the command is silently ignored
578 if (index == -1)
579 return;
580
581 const auto &binding = getFragmentInputBindingInfo(index);
582
583 // If the input doesn't exist then then the command is silently ignored
584 // This could happen through optimization for example, the shader translator
585 // decides that a variable is not actually being used and optimizes it away.
586 if (binding.name.empty())
587 return;
588
589 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
590}
591
Martin Radev4c4c8e72016-08-04 12:25:34 +0300592// The attached shaders are checked for linking errors by matching up their variables.
593// Uniform, input and output variables get collected.
594// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500595Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000596{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500597 const auto &data = context->getContextState();
598
Jamie Madill6c1f6712017-02-14 19:08:04 -0500599 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000600
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000601 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000602 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000603
Martin Radev4c4c8e72016-08-04 12:25:34 +0300604 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500605
Jamie Madill192745a2016-12-22 15:58:21 -0500606 auto vertexShader = mState.mAttachedVertexShader;
607 auto fragmentShader = mState.mAttachedFragmentShader;
608 auto computeShader = mState.mAttachedComputeShader;
609
610 bool isComputeShaderAttached = (computeShader != nullptr);
611 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300612 // Check whether we both have a compute and non-compute shaders attached.
613 // If there are of both types attached, then linking should fail.
614 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
615 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500616 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300617 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
618 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400619 }
620
Jamie Madill192745a2016-12-22 15:58:21 -0500621 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500622 {
Jamie Madill192745a2016-12-22 15:58:21 -0500623 if (!computeShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300624 {
625 mInfoLog << "Attached compute shader is not compiled.";
626 return NoError();
627 }
Jamie Madill192745a2016-12-22 15:58:21 -0500628 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300629
Jamie Madill192745a2016-12-22 15:58:21 -0500630 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300631
632 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
633 // If the work group size is not specified, a link time error should occur.
634 if (!mState.mComputeShaderLocalSize.isDeclared())
635 {
636 mInfoLog << "Work group size is not specified.";
637 return NoError();
638 }
639
640 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
641 {
642 return NoError();
643 }
644
645 if (!linkUniformBlocks(mInfoLog, caps))
646 {
647 return NoError();
648 }
649
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500650 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
651 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), noPacking, mInfoLog),
652 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500653 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300654 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500655 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300656 }
657 }
658 else
659 {
Jamie Madill192745a2016-12-22 15:58:21 -0500660 if (!fragmentShader || !fragmentShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300661 {
662 return NoError();
663 }
Jamie Madill192745a2016-12-22 15:58:21 -0500664 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300665
Jamie Madill192745a2016-12-22 15:58:21 -0500666 if (!vertexShader || !vertexShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300667 {
668 return NoError();
669 }
Jamie Madill192745a2016-12-22 15:58:21 -0500670 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300671
Jamie Madill192745a2016-12-22 15:58:21 -0500672 if (fragmentShader->getShaderVersion() != vertexShader->getShaderVersion())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300673 {
674 mInfoLog << "Fragment shader version does not match vertex shader version.";
675 return NoError();
676 }
677
Jamie Madilleb979bf2016-11-15 12:28:46 -0500678 if (!linkAttributes(data, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300679 {
680 return NoError();
681 }
682
Jamie Madill192745a2016-12-22 15:58:21 -0500683 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300684 {
685 return NoError();
686 }
687
688 if (!linkUniforms(mInfoLog, caps, mUniformBindings))
689 {
690 return NoError();
691 }
692
693 if (!linkUniformBlocks(mInfoLog, caps))
694 {
695 return NoError();
696 }
697
698 const auto &mergedVaryings = getMergedVaryings();
699
700 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
701 {
702 return NoError();
703 }
704
705 linkOutputVariables();
706
Jamie Madill192745a2016-12-22 15:58:21 -0500707 // Validate we can pack the varyings.
708 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
709
710 // Map the varyings to the register file
711 // In WebGL, we use a slightly different handling for packing variables.
712 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
713 : PackMode::ANGLE_RELAXED;
714 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
715 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
716 mState.getTransformFeedbackVaryingNames()))
717 {
718 return NoError();
719 }
720
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500721 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), varyingPacking, mInfoLog),
722 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500723 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300724 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500725 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300726 }
727
728 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500729 }
730
Jamie Madill4a3c2342015-10-08 12:58:45 -0400731 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400732
Martin Radev4c4c8e72016-08-04 12:25:34 +0300733 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000734}
735
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000736// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500737void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400739 mState.mAttributes.clear();
740 mState.mActiveAttribLocationsMask.reset();
741 mState.mTransformFeedbackVaryingVars.clear();
742 mState.mUniforms.clear();
743 mState.mUniformLocations.clear();
744 mState.mUniformBlocks.clear();
745 mState.mOutputVariables.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300746 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500747 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500748
Geoff Lang7dd2e102014-11-10 15:19:26 -0500749 mValidated = false;
750
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000751 mLinked = false;
752}
753
Geoff Lange1a27752015-10-05 13:16:04 -0400754bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000755{
756 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757}
758
Jamie Madilla2c74982016-12-12 11:20:42 -0500759Error Program::loadBinary(const Context *context,
760 GLenum binaryFormat,
761 const void *binary,
762 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000763{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500764 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000765
Geoff Lang7dd2e102014-11-10 15:19:26 -0500766#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800767 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500768#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400769 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
770 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000771 {
Jamie Madillf6113162015-05-07 11:49:21 -0400772 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800773 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500774 }
775
Geoff Langc46cc2f2015-10-01 17:16:20 -0400776 BinaryInputStream stream(binary, length);
777
Jamie Madilla2c74982016-12-12 11:20:42 -0500778 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
779 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
780 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) !=
781 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500782 {
Jamie Madillf6113162015-05-07 11:49:21 -0400783 mInfoLog << "Invalid program binary version.";
He Yunchaoacd18982017-01-04 10:46:42 +0800784 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500785 }
786
Jamie Madilla2c74982016-12-12 11:20:42 -0500787 int majorVersion = stream.readInt<int>();
788 int minorVersion = stream.readInt<int>();
789 if (majorVersion != context->getClientMajorVersion() ||
790 minorVersion != context->getClientMinorVersion())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500791 {
Jamie Madilla2c74982016-12-12 11:20:42 -0500792 mInfoLog << "Cannot load program binaries across different ES context versions.";
He Yunchaoacd18982017-01-04 10:46:42 +0800793 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500794 }
795
Martin Radev4c4c8e72016-08-04 12:25:34 +0300796 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
797 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
798 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
799
Jamie Madill63805b42015-08-25 13:17:39 -0400800 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
801 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400802 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500803
Jamie Madill3da79b72015-04-27 11:09:17 -0400804 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400805 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400806 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
807 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400808 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400809 LoadShaderVar(&stream, &attrib);
810 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400811 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400812 }
813
Jamie Madill62d31cb2015-09-11 13:25:51 -0400814 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400815 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400816 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
817 {
818 LinkedUniform uniform;
819 LoadShaderVar(&stream, &uniform);
820
821 uniform.blockIndex = stream.readInt<int>();
822 uniform.blockInfo.offset = stream.readInt<int>();
823 uniform.blockInfo.arrayStride = stream.readInt<int>();
824 uniform.blockInfo.matrixStride = stream.readInt<int>();
825 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
826
Jamie Madill48ef11b2016-04-27 15:21:52 -0400827 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400828 }
829
830 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400831 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400832 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
833 uniformIndexIndex++)
834 {
835 VariableLocation variable;
836 stream.readString(&variable.name);
837 stream.readInt(&variable.element);
838 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400839 stream.readBool(&variable.used);
840 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400841
Jamie Madill48ef11b2016-04-27 15:21:52 -0400842 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400843 }
844
845 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400846 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400847 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
848 ++uniformBlockIndex)
849 {
850 UniformBlock uniformBlock;
851 stream.readString(&uniformBlock.name);
852 stream.readBool(&uniformBlock.isArray);
853 stream.readInt(&uniformBlock.arrayElement);
854 stream.readInt(&uniformBlock.dataSize);
855 stream.readBool(&uniformBlock.vertexStaticUse);
856 stream.readBool(&uniformBlock.fragmentStaticUse);
857
858 unsigned int numMembers = stream.readInt<unsigned int>();
859 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
860 {
861 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
862 }
863
Jamie Madill48ef11b2016-04-27 15:21:52 -0400864 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400865 }
866
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500867 for (GLuint bindingIndex = 0; bindingIndex < mState.mUniformBlockBindings.size();
868 ++bindingIndex)
869 {
870 stream.readInt(&mState.mUniformBlockBindings[bindingIndex]);
871 mState.mActiveUniformBlockBindings.set(bindingIndex,
872 mState.mUniformBlockBindings[bindingIndex] != 0);
873 }
874
Brandon Jones1048ea72015-10-06 15:34:52 -0700875 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400876 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700877 for (unsigned int transformFeedbackVaryingIndex = 0;
878 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
879 ++transformFeedbackVaryingIndex)
880 {
881 sh::Varying varying;
882 stream.readInt(&varying.arraySize);
883 stream.readInt(&varying.type);
884 stream.readString(&varying.name);
885
Jamie Madill48ef11b2016-04-27 15:21:52 -0400886 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700887 }
888
Jamie Madill48ef11b2016-04-27 15:21:52 -0400889 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400890
Jamie Madill80a6fc02015-08-21 16:53:16 -0400891 unsigned int outputVarCount = stream.readInt<unsigned int>();
892 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
893 {
894 int locationIndex = stream.readInt<int>();
895 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400896 stream.readInt(&locationData.element);
897 stream.readInt(&locationData.index);
898 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400899 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400900 }
901
Jamie Madille7d84322017-01-10 18:21:59 -0500902 stream.readInt(&mState.mSamplerUniformRange.start);
903 stream.readInt(&mState.mSamplerUniformRange.end);
904
905 unsigned int samplerCount = stream.readInt<unsigned int>();
906 for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex)
907 {
908 GLenum textureType = stream.readInt<GLenum>();
909 size_t bindingCount = stream.readInt<size_t>();
910 mState.mSamplerBindings.emplace_back(SamplerBinding(textureType, bindingCount));
911 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400912
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500913 ANGLE_TRY_RESULT(mProgram->load(context->getImplementation(), mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000914
Jamie Madillb0a838b2016-11-13 20:02:12 -0500915 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500916#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500917}
918
Jamie Madilla2c74982016-12-12 11:20:42 -0500919Error Program::saveBinary(const Context *context,
920 GLenum *binaryFormat,
921 void *binary,
922 GLsizei bufSize,
923 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500924{
925 if (binaryFormat)
926 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400927 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500928 }
929
930 BinaryOutputStream stream;
931
Geoff Lang7dd2e102014-11-10 15:19:26 -0500932 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
933
Jamie Madilla2c74982016-12-12 11:20:42 -0500934 // nullptr context is supported when computing binary length.
935 if (context)
936 {
937 stream.writeInt(context->getClientVersion().major);
938 stream.writeInt(context->getClientVersion().minor);
939 }
940 else
941 {
942 stream.writeInt(2);
943 stream.writeInt(0);
944 }
945
Martin Radev4c4c8e72016-08-04 12:25:34 +0300946 stream.writeInt(mState.mComputeShaderLocalSize[0]);
947 stream.writeInt(mState.mComputeShaderLocalSize[1]);
948 stream.writeInt(mState.mComputeShaderLocalSize[2]);
949
Jamie Madill48ef11b2016-04-27 15:21:52 -0400950 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500951
Jamie Madill48ef11b2016-04-27 15:21:52 -0400952 stream.writeInt(mState.mAttributes.size());
953 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400954 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400955 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400956 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400957 }
958
Jamie Madill48ef11b2016-04-27 15:21:52 -0400959 stream.writeInt(mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -0500960 for (const LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400961 {
962 WriteShaderVar(&stream, uniform);
963
964 // FIXME: referenced
965
966 stream.writeInt(uniform.blockIndex);
967 stream.writeInt(uniform.blockInfo.offset);
968 stream.writeInt(uniform.blockInfo.arrayStride);
969 stream.writeInt(uniform.blockInfo.matrixStride);
970 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
971 }
972
Jamie Madill48ef11b2016-04-27 15:21:52 -0400973 stream.writeInt(mState.mUniformLocations.size());
974 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400975 {
976 stream.writeString(variable.name);
977 stream.writeInt(variable.element);
978 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400979 stream.writeInt(variable.used);
980 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400981 }
982
Jamie Madill48ef11b2016-04-27 15:21:52 -0400983 stream.writeInt(mState.mUniformBlocks.size());
984 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400985 {
986 stream.writeString(uniformBlock.name);
987 stream.writeInt(uniformBlock.isArray);
988 stream.writeInt(uniformBlock.arrayElement);
989 stream.writeInt(uniformBlock.dataSize);
990
991 stream.writeInt(uniformBlock.vertexStaticUse);
992 stream.writeInt(uniformBlock.fragmentStaticUse);
993
994 stream.writeInt(uniformBlock.memberUniformIndexes.size());
995 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
996 {
997 stream.writeInt(memberUniformIndex);
998 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400999 }
1000
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001001 for (GLuint binding : mState.mUniformBlockBindings)
1002 {
1003 stream.writeInt(binding);
1004 }
1005
Jamie Madill48ef11b2016-04-27 15:21:52 -04001006 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
1007 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -07001008 {
1009 stream.writeInt(varying.arraySize);
1010 stream.writeInt(varying.type);
1011 stream.writeString(varying.name);
1012 }
1013
Jamie Madill48ef11b2016-04-27 15:21:52 -04001014 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -04001015
Jamie Madill48ef11b2016-04-27 15:21:52 -04001016 stream.writeInt(mState.mOutputVariables.size());
1017 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -04001018 {
1019 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -04001020 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001021 stream.writeInt(outputPair.second.index);
1022 stream.writeString(outputPair.second.name);
1023 }
1024
Jamie Madille7d84322017-01-10 18:21:59 -05001025 stream.writeInt(mState.mSamplerUniformRange.start);
1026 stream.writeInt(mState.mSamplerUniformRange.end);
1027
1028 stream.writeInt(mState.mSamplerBindings.size());
1029 for (const auto &samplerBinding : mState.mSamplerBindings)
1030 {
1031 stream.writeInt(samplerBinding.textureType);
1032 stream.writeInt(samplerBinding.boundTextureUnits.size());
1033 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001034
Jamie Madilla2c74982016-12-12 11:20:42 -05001035 ANGLE_TRY(mProgram->save(&stream));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001036
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001037 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -04001038 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001039
1040 if (streamLength > bufSize)
1041 {
1042 if (length)
1043 {
1044 *length = 0;
1045 }
1046
1047 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1048 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1049 // sizes and then copy it.
1050 return Error(GL_INVALID_OPERATION);
1051 }
1052
1053 if (binary)
1054 {
1055 char *ptr = reinterpret_cast<char*>(binary);
1056
Jamie Madill48ef11b2016-04-27 15:21:52 -04001057 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001058 ptr += streamLength;
1059
1060 ASSERT(ptr - streamLength == binary);
1061 }
1062
1063 if (length)
1064 {
1065 *length = streamLength;
1066 }
1067
He Yunchaoacd18982017-01-04 10:46:42 +08001068 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001069}
1070
1071GLint Program::getBinaryLength() const
1072{
1073 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -05001074 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001075 if (error.isError())
1076 {
1077 return 0;
1078 }
1079
1080 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001081}
1082
Geoff Langc5629752015-12-07 16:29:04 -05001083void Program::setBinaryRetrievableHint(bool retrievable)
1084{
1085 // TODO(jmadill) : replace with dirty bits
1086 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001087 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001088}
1089
1090bool Program::getBinaryRetrievableHint() const
1091{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001092 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001093}
1094
Jamie Madill6c1f6712017-02-14 19:08:04 -05001095void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001096{
1097 mRefCount--;
1098
1099 if (mRefCount == 0 && mDeleteStatus)
1100 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001101 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001102 }
1103}
1104
1105void Program::addRef()
1106{
1107 mRefCount++;
1108}
1109
1110unsigned int Program::getRefCount() const
1111{
1112 return mRefCount;
1113}
1114
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001115int Program::getInfoLogLength() const
1116{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001117 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001118}
1119
Geoff Lange1a27752015-10-05 13:16:04 -04001120void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001121{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001122 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001123}
1124
Geoff Lange1a27752015-10-05 13:16:04 -04001125void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001126{
1127 int total = 0;
1128
Martin Radev4c4c8e72016-08-04 12:25:34 +03001129 if (mState.mAttachedComputeShader)
1130 {
1131 if (total < maxCount)
1132 {
1133 shaders[total] = mState.mAttachedComputeShader->getHandle();
1134 total++;
1135 }
1136 }
1137
Jamie Madill48ef11b2016-04-27 15:21:52 -04001138 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001139 {
1140 if (total < maxCount)
1141 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001142 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001143 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001144 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001145 }
1146
Jamie Madill48ef11b2016-04-27 15:21:52 -04001147 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001148 {
1149 if (total < maxCount)
1150 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001151 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001152 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001153 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001154 }
1155
1156 if (count)
1157 {
1158 *count = total;
1159 }
1160}
1161
Geoff Lange1a27752015-10-05 13:16:04 -04001162GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001163{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001164 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001165 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001166 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001167 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001168 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001169 }
1170 }
1171
Austin Kinrossb8af7232015-03-16 22:33:25 -07001172 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001173}
1174
Jamie Madill63805b42015-08-25 13:17:39 -04001175bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001176{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001177 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1178 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001179}
1180
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001181void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1182{
Jamie Madillc349ec02015-08-21 16:53:12 -04001183 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001184 {
1185 if (bufsize > 0)
1186 {
1187 name[0] = '\0';
1188 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001189
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001190 if (length)
1191 {
1192 *length = 0;
1193 }
1194
1195 *type = GL_NONE;
1196 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001197 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001198 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001199
1200 size_t attributeIndex = 0;
1201
Jamie Madill48ef11b2016-04-27 15:21:52 -04001202 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001203 {
1204 // Skip over inactive attributes
1205 if (attribute.staticUse)
1206 {
1207 if (static_cast<size_t>(index) == attributeIndex)
1208 {
1209 break;
1210 }
1211 attributeIndex++;
1212 }
1213 }
1214
Jamie Madill48ef11b2016-04-27 15:21:52 -04001215 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1216 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001217
1218 if (bufsize > 0)
1219 {
1220 const char *string = attrib.name.c_str();
1221
1222 strncpy(name, string, bufsize);
1223 name[bufsize - 1] = '\0';
1224
1225 if (length)
1226 {
1227 *length = static_cast<GLsizei>(strlen(name));
1228 }
1229 }
1230
1231 // Always a single 'type' instance
1232 *size = 1;
1233 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001234}
1235
Geoff Lange1a27752015-10-05 13:16:04 -04001236GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001237{
Jamie Madillc349ec02015-08-21 16:53:12 -04001238 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001239 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001240 return 0;
1241 }
1242
1243 GLint count = 0;
1244
Jamie Madill48ef11b2016-04-27 15:21:52 -04001245 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001246 {
1247 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001248 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001249
1250 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001251}
1252
Geoff Lange1a27752015-10-05 13:16:04 -04001253GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001254{
Jamie Madillc349ec02015-08-21 16:53:12 -04001255 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001256 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001257 return 0;
1258 }
1259
1260 size_t maxLength = 0;
1261
Jamie Madill48ef11b2016-04-27 15:21:52 -04001262 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001263 {
1264 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001265 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001266 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001267 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001268 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001269
Jamie Madillc349ec02015-08-21 16:53:12 -04001270 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271}
1272
Geoff Lang7dd2e102014-11-10 15:19:26 -05001273GLint Program::getFragDataLocation(const std::string &name) const
1274{
1275 std::string baseName(name);
1276 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001277 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001278 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001279 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001280 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1281 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001282 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001283 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001284 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001285 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001286}
1287
Geoff Lange1a27752015-10-05 13:16:04 -04001288void Program::getActiveUniform(GLuint index,
1289 GLsizei bufsize,
1290 GLsizei *length,
1291 GLint *size,
1292 GLenum *type,
1293 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001294{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001295 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001296 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001297 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001298 ASSERT(index < mState.mUniforms.size());
1299 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001300
1301 if (bufsize > 0)
1302 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001303 std::string string = uniform.name;
1304 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305 {
1306 string += "[0]";
1307 }
1308
1309 strncpy(name, string.c_str(), bufsize);
1310 name[bufsize - 1] = '\0';
1311
1312 if (length)
1313 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001314 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001315 }
1316 }
1317
Jamie Madill62d31cb2015-09-11 13:25:51 -04001318 *size = uniform.elementCount();
1319 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001320 }
1321 else
1322 {
1323 if (bufsize > 0)
1324 {
1325 name[0] = '\0';
1326 }
1327
1328 if (length)
1329 {
1330 *length = 0;
1331 }
1332
1333 *size = 0;
1334 *type = GL_NONE;
1335 }
1336}
1337
Geoff Lange1a27752015-10-05 13:16:04 -04001338GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001339{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001340 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001341 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001342 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001343 }
1344 else
1345 {
1346 return 0;
1347 }
1348}
1349
Geoff Lange1a27752015-10-05 13:16:04 -04001350GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001351{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001352 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001353
1354 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001355 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001356 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001358 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001360 size_t length = uniform.name.length() + 1u;
1361 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001362 {
1363 length += 3; // Counting in "[0]".
1364 }
1365 maxLength = std::max(length, maxLength);
1366 }
1367 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001368 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001369
Jamie Madill62d31cb2015-09-11 13:25:51 -04001370 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001371}
1372
1373GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1374{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001375 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001376 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001378 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1380 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1381 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1382 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1383 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1384 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1385 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1386 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1387 default:
1388 UNREACHABLE();
1389 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001390 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001391 return 0;
1392}
1393
1394bool Program::isValidUniformLocation(GLint location) const
1395{
Jamie Madille2e406c2016-06-02 13:04:10 -04001396 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001397 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1398 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001399}
1400
Jamie Madill62d31cb2015-09-11 13:25:51 -04001401const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001403 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001404 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001405}
1406
Jamie Madillac4e9c32017-01-13 14:07:12 -05001407const VariableLocation &Program::getUniformLocation(GLint location) const
1408{
1409 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1410 return mState.mUniformLocations[location];
1411}
1412
1413const std::vector<VariableLocation> &Program::getUniformLocations() const
1414{
1415 return mState.mUniformLocations;
1416}
1417
1418const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1419{
1420 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1421 return mState.mUniforms[index];
1422}
1423
Jamie Madill62d31cb2015-09-11 13:25:51 -04001424GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001425{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001426 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427}
1428
Jamie Madill62d31cb2015-09-11 13:25:51 -04001429GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001430{
Jamie Madille7d84322017-01-10 18:21:59 -05001431 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432}
1433
1434void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1435{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001436 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1437 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438}
1439
1440void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1441{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001442 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1443 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444}
1445
1446void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1447{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001448 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1449 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450}
1451
1452void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1453{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001454 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1455 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456}
1457
1458void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1459{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001460 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1461 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462}
1463
1464void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1465{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001466 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1467 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001468}
1469
1470void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1471{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001472 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1473 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474}
1475
1476void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1477{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001478 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1479 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480}
1481
1482void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1483{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001484 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1485 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486}
1487
1488void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1489{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001490 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1491 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492}
1493
1494void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1495{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001496 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1497 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001498}
1499
1500void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1501{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001502 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1503 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504}
1505
1506void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1507{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001508 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1509 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001510}
1511
1512void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1513{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001514 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1515 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001516}
1517
1518void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1519{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001520 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1521 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001522}
1523
1524void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1525{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001526 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1527 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001528}
1529
1530void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1531{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001532 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1533 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001534}
1535
1536void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1537{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001538 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1539 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001540}
1541
1542void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1543{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001544 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1545 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001546}
1547
1548void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1549{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001550 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1551 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001552}
1553
1554void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1555{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001556 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1557 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001558}
1559
Geoff Lange1a27752015-10-05 13:16:04 -04001560void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001561{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001562 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001563}
1564
Geoff Lange1a27752015-10-05 13:16:04 -04001565void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001566{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001567 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001568}
1569
Geoff Lange1a27752015-10-05 13:16:04 -04001570void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001571{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001572 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001573}
1574
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001575void Program::flagForDeletion()
1576{
1577 mDeleteStatus = true;
1578}
1579
1580bool Program::isFlaggedForDeletion() const
1581{
1582 return mDeleteStatus;
1583}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001584
Brandon Jones43a53e22014-08-28 16:23:22 -07001585void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001586{
1587 mInfoLog.reset();
1588
Geoff Lang7dd2e102014-11-10 15:19:26 -05001589 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001590 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001591 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001592 }
1593 else
1594 {
Jamie Madillf6113162015-05-07 11:49:21 -04001595 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001596 }
1597}
1598
Geoff Lang7dd2e102014-11-10 15:19:26 -05001599bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1600{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001601 // Skip cache if we're using an infolog, so we get the full error.
1602 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1603 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1604 {
1605 return mCachedValidateSamplersResult.value();
1606 }
1607
1608 if (mTextureUnitTypesCache.empty())
1609 {
1610 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1611 }
1612 else
1613 {
1614 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1615 }
1616
1617 // if any two active samplers in a program are of different types, but refer to the same
1618 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1619 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001620 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001621 {
Jamie Madille7d84322017-01-10 18:21:59 -05001622 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001623
Jamie Madille7d84322017-01-10 18:21:59 -05001624 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001625 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001626 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1627 {
1628 if (infoLog)
1629 {
1630 (*infoLog) << "Sampler uniform (" << textureUnit
1631 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1632 << caps.maxCombinedTextureImageUnits << ")";
1633 }
1634
1635 mCachedValidateSamplersResult = false;
1636 return false;
1637 }
1638
1639 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1640 {
1641 if (textureType != mTextureUnitTypesCache[textureUnit])
1642 {
1643 if (infoLog)
1644 {
1645 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1646 "image unit ("
1647 << textureUnit << ").";
1648 }
1649
1650 mCachedValidateSamplersResult = false;
1651 return false;
1652 }
1653 }
1654 else
1655 {
1656 mTextureUnitTypesCache[textureUnit] = textureType;
1657 }
1658 }
1659 }
1660
1661 mCachedValidateSamplersResult = true;
1662 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001663}
1664
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001665bool Program::isValidated() const
1666{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667 return mValidated;
1668}
1669
Geoff Lange1a27752015-10-05 13:16:04 -04001670GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001671{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001672 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001673}
1674
1675void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1676{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001677 ASSERT(
1678 uniformBlockIndex <
1679 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680
Jamie Madill48ef11b2016-04-27 15:21:52 -04001681 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001682
1683 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001684 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001685 std::string string = uniformBlock.name;
1686
Jamie Madill62d31cb2015-09-11 13:25:51 -04001687 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001688 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001689 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001690 }
1691
1692 strncpy(uniformBlockName, string.c_str(), bufSize);
1693 uniformBlockName[bufSize - 1] = '\0';
1694
1695 if (length)
1696 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001697 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001698 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001699 }
1700}
1701
Geoff Lange1a27752015-10-05 13:16:04 -04001702GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001703{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001704 int maxLength = 0;
1705
1706 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001707 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001708 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001709 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1710 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001711 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001712 if (!uniformBlock.name.empty())
1713 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001714 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001715
1716 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001717 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001718
1719 maxLength = std::max(length + arrayLength, maxLength);
1720 }
1721 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001722 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001723
1724 return maxLength;
1725}
1726
Geoff Lange1a27752015-10-05 13:16:04 -04001727GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001728{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001729 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -05001730 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001731
Jamie Madill48ef11b2016-04-27 15:21:52 -04001732 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001733 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1734 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001735 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001736 if (uniformBlock.name == baseName)
1737 {
1738 const bool arrayElementZero =
1739 (subscript == GL_INVALID_INDEX &&
1740 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1741 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1742 {
1743 return blockIndex;
1744 }
1745 }
1746 }
1747
1748 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001749}
1750
Jamie Madill62d31cb2015-09-11 13:25:51 -04001751const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001752{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001753 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1754 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001755}
1756
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001757void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1758{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001759 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001760 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001761 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001762}
1763
1764GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1765{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001766 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001767}
1768
1769void Program::resetUniformBlockBindings()
1770{
1771 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1772 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001773 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001774 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001775 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001776}
1777
Geoff Lang48dcae72014-02-05 16:28:24 -05001778void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1779{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001780 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001781 for (GLsizei i = 0; i < count; i++)
1782 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001783 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001784 }
1785
Jamie Madill48ef11b2016-04-27 15:21:52 -04001786 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001787}
1788
1789void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1790{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001791 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001792 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001793 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1794 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001795 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1796 if (length)
1797 {
1798 *length = lastNameIdx;
1799 }
1800 if (size)
1801 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001802 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001803 }
1804 if (type)
1805 {
1806 *type = varying.type;
1807 }
1808 if (name)
1809 {
1810 memcpy(name, varying.name.c_str(), lastNameIdx);
1811 name[lastNameIdx] = '\0';
1812 }
1813 }
1814}
1815
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001816GLsizei Program::getTransformFeedbackVaryingCount() const
1817{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001818 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001819 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001820 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001821 }
1822 else
1823 {
1824 return 0;
1825 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001826}
1827
1828GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1829{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001830 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001831 {
1832 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001833 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001834 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001835 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1836 }
1837
1838 return maxSize;
1839 }
1840 else
1841 {
1842 return 0;
1843 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001844}
1845
1846GLenum Program::getTransformFeedbackBufferMode() const
1847{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001848 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001849}
1850
Jamie Madill192745a2016-12-22 15:58:21 -05001851bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001852{
Jamie Madill192745a2016-12-22 15:58:21 -05001853 const Shader *vertexShader = mState.mAttachedVertexShader;
1854 const Shader *fragmentShader = mState.mAttachedFragmentShader;
1855
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001856 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1857
Jamie Madill4cff2472015-08-21 16:53:18 -04001858 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1859 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001860
Sami Väisänen46eaa942016-06-29 10:26:37 +03001861 std::map<GLuint, std::string> staticFragmentInputLocations;
1862
Jamie Madill4cff2472015-08-21 16:53:18 -04001863 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001864 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001865 bool matched = false;
1866
1867 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001868 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001869 {
1870 continue;
1871 }
1872
Jamie Madill4cff2472015-08-21 16:53:18 -04001873 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001874 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001875 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001876 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001877 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001878 if (!linkValidateVaryings(infoLog, output.name, input, output,
1879 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001880 {
1881 return false;
1882 }
1883
Geoff Lang7dd2e102014-11-10 15:19:26 -05001884 matched = true;
1885 break;
1886 }
1887 }
1888
1889 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001890 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001891 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001892 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001893 return false;
1894 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001895
1896 // Check for aliased path rendering input bindings (if any).
1897 // If more than one binding refer statically to the same
1898 // location the link must fail.
1899
1900 if (!output.staticUse)
1901 continue;
1902
1903 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1904 if (inputBinding == -1)
1905 continue;
1906
1907 const auto it = staticFragmentInputLocations.find(inputBinding);
1908 if (it == std::end(staticFragmentInputLocations))
1909 {
1910 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1911 }
1912 else
1913 {
1914 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1915 << it->second;
1916 return false;
1917 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001918 }
1919
Jamie Madillada9ecc2015-08-17 12:53:37 -04001920 // TODO(jmadill): verify no unmatched vertex varyings?
1921
Geoff Lang7dd2e102014-11-10 15:19:26 -05001922 return true;
1923}
1924
Martin Radev4c4c8e72016-08-04 12:25:34 +03001925bool Program::validateVertexAndFragmentUniforms(InfoLog &infoLog) const
Jamie Madillea918db2015-08-18 14:48:59 -04001926{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001927 // Check that uniforms defined in the vertex and fragment shaders are identical
1928 std::map<std::string, LinkedUniform> linkedUniforms;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001929 const std::vector<sh::Uniform> &vertexUniforms = mState.mAttachedVertexShader->getUniforms();
1930 const std::vector<sh::Uniform> &fragmentUniforms =
1931 mState.mAttachedFragmentShader->getUniforms();
Jamie Madillea918db2015-08-18 14:48:59 -04001932
Jamie Madillea918db2015-08-18 14:48:59 -04001933 for (const sh::Uniform &vertexUniform : vertexUniforms)
1934 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001935 linkedUniforms[vertexUniform.name] = LinkedUniform(vertexUniform);
Jamie Madillea918db2015-08-18 14:48:59 -04001936 }
1937
1938 for (const sh::Uniform &fragmentUniform : fragmentUniforms)
1939 {
1940 auto entry = linkedUniforms.find(fragmentUniform.name);
1941 if (entry != linkedUniforms.end())
1942 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001943 LinkedUniform *vertexUniform = &entry->second;
1944 const std::string &uniformName = "uniform '" + vertexUniform->name + "'";
1945 if (!linkValidateUniforms(infoLog, uniformName, *vertexUniform, fragmentUniform))
Jamie Madillea918db2015-08-18 14:48:59 -04001946 {
1947 return false;
1948 }
1949 }
1950 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03001951 return true;
1952}
1953
Jamie Madilla2c74982016-12-12 11:20:42 -05001954bool Program::linkUniforms(InfoLog &infoLog, const Caps &caps, const Bindings &uniformBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001955{
1956 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1957 {
1958 ASSERT(mState.mAttachedComputeShader == nullptr);
1959 if (!validateVertexAndFragmentUniforms(infoLog))
1960 {
1961 return false;
1962 }
1963 }
Jamie Madillea918db2015-08-18 14:48:59 -04001964
Jamie Madill62d31cb2015-09-11 13:25:51 -04001965 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1966 // Also check the maximum uniform vector and sampler counts.
1967 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1968 {
1969 return false;
1970 }
1971
Geoff Langd8605522016-04-13 10:19:12 -04001972 if (!indexUniforms(infoLog, caps, uniformBindings))
1973 {
1974 return false;
1975 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001976
Jamie Madillea918db2015-08-18 14:48:59 -04001977 return true;
1978}
1979
Jamie Madilla2c74982016-12-12 11:20:42 -05001980bool Program::indexUniforms(InfoLog &infoLog, const Caps &caps, const Bindings &uniformBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001981{
Geoff Langd8605522016-04-13 10:19:12 -04001982 // Uniforms awaiting a location
1983 std::vector<VariableLocation> unboundUniforms;
1984 std::map<GLuint, VariableLocation> boundUniforms;
1985 int maxUniformLocation = -1;
1986
1987 // Gather bound and unbound uniforms
Jamie Madill48ef11b2016-04-27 15:21:52 -04001988 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001989 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001990 const LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001991
Geoff Langd8605522016-04-13 10:19:12 -04001992 if (uniform.isBuiltIn())
1993 {
1994 continue;
1995 }
1996
1997 int bindingLocation = uniformBindings.getBinding(uniform.name);
1998
1999 // Verify that this location isn't bound twice
2000 if (bindingLocation != -1 && boundUniforms.find(bindingLocation) != boundUniforms.end())
2001 {
2002 infoLog << "Multiple uniforms bound to location " << bindingLocation << ".";
2003 return false;
2004 }
2005
Jamie Madill62d31cb2015-09-11 13:25:51 -04002006 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
2007 {
Geoff Langd8605522016-04-13 10:19:12 -04002008 VariableLocation location(uniform.name, arrayIndex,
2009 static_cast<unsigned int>(uniformIndex));
2010
2011 if (arrayIndex == 0 && bindingLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002012 {
Geoff Langd8605522016-04-13 10:19:12 -04002013 boundUniforms[bindingLocation] = location;
2014 maxUniformLocation = std::max(maxUniformLocation, bindingLocation);
2015 }
2016 else
2017 {
2018 unboundUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002019 }
2020 }
2021 }
Geoff Langd8605522016-04-13 10:19:12 -04002022
2023 // Gather the reserved bindings, ones that are bound but not referenced. Other uniforms should
2024 // not be assigned to those locations.
2025 std::set<GLuint> reservedLocations;
2026 for (const auto &binding : uniformBindings)
2027 {
2028 GLuint location = binding.second;
2029 if (boundUniforms.find(location) == boundUniforms.end())
2030 {
2031 reservedLocations.insert(location);
2032 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
2033 }
2034 }
2035
2036 // Make enough space for all uniforms, bound and unbound
Jamie Madill48ef11b2016-04-27 15:21:52 -04002037 mState.mUniformLocations.resize(
Geoff Langd8605522016-04-13 10:19:12 -04002038 std::max(unboundUniforms.size() + boundUniforms.size() + reservedLocations.size(),
2039 static_cast<size_t>(maxUniformLocation + 1)));
2040
2041 // Assign bound uniforms
2042 for (const auto &boundUniform : boundUniforms)
2043 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002044 mState.mUniformLocations[boundUniform.first] = boundUniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04002045 }
2046
2047 // Assign reserved uniforms
2048 for (const auto &reservedLocation : reservedLocations)
2049 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002050 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04002051 }
2052
2053 // Assign unbound uniforms
2054 size_t nextUniformLocation = 0;
2055 for (const auto &unboundUniform : unboundUniforms)
2056 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002057 while (mState.mUniformLocations[nextUniformLocation].used ||
2058 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04002059 {
2060 nextUniformLocation++;
2061 }
2062
Jamie Madill48ef11b2016-04-27 15:21:52 -04002063 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
2064 mState.mUniformLocations[nextUniformLocation] = unboundUniform;
Geoff Langd8605522016-04-13 10:19:12 -04002065 nextUniformLocation++;
2066 }
2067
2068 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002069}
2070
Martin Radev4c4c8e72016-08-04 12:25:34 +03002071bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2072 const std::string &uniformName,
2073 const sh::InterfaceBlockField &vertexUniform,
2074 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075{
Jamie Madillc4c744222015-11-04 09:39:47 -05002076 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2077 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 {
2079 return false;
2080 }
2081
2082 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2083 {
Jamie Madillf6113162015-05-07 11:49:21 -04002084 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002085 return false;
2086 }
2087
2088 return true;
2089}
2090
Jamie Madilleb979bf2016-11-15 12:28:46 -05002091// Assigns locations to all attributes from the bindings and program locations.
2092bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002093{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002094 const auto *vertexShader = mState.getAttachedVertexShader();
2095
Geoff Lang7dd2e102014-11-10 15:19:26 -05002096 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002097 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002098 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002099
2100 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002101 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002102 {
Jamie Madillf6113162015-05-07 11:49:21 -04002103 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002104 return false;
2105 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002106
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002107 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002108
Jamie Madillc349ec02015-08-21 16:53:12 -04002109 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002110 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002111 {
2112 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113 ASSERT(attribute.staticUse);
2114
Jamie Madilleb979bf2016-11-15 12:28:46 -05002115 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002116 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002117 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002118 attribute.location = bindingLocation;
2119 }
2120
2121 if (attribute.location != -1)
2122 {
2123 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002124 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125
Jamie Madill63805b42015-08-25 13:17:39 -04002126 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002127 {
Jamie Madillf6113162015-05-07 11:49:21 -04002128 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002129 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130
2131 return false;
2132 }
2133
Jamie Madill63805b42015-08-25 13:17:39 -04002134 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002135 {
Jamie Madill63805b42015-08-25 13:17:39 -04002136 const int regLocation = attribute.location + reg;
2137 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002138
2139 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002140 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002141 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002143 // TODO(jmadill): fix aliasing on ES2
2144 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002145 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002146 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002147 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002148 return false;
2149 }
2150 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002151 else
2152 {
Jamie Madill63805b42015-08-25 13:17:39 -04002153 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002154 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002155
Jamie Madill63805b42015-08-25 13:17:39 -04002156 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157 }
2158 }
2159 }
2160
2161 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002162 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002164 ASSERT(attribute.staticUse);
2165
Jamie Madillc349ec02015-08-21 16:53:12 -04002166 // Not set by glBindAttribLocation or by location layout qualifier
2167 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002168 {
Jamie Madill63805b42015-08-25 13:17:39 -04002169 int regs = VariableRegisterCount(attribute.type);
2170 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002171
Jamie Madill63805b42015-08-25 13:17:39 -04002172 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173 {
Jamie Madillf6113162015-05-07 11:49:21 -04002174 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002175 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002176 }
2177
Jamie Madillc349ec02015-08-21 16:53:12 -04002178 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002179 }
2180 }
2181
Jamie Madill48ef11b2016-04-27 15:21:52 -04002182 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002184 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002185 ASSERT(attribute.location != -1);
2186 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002187
Jamie Madill63805b42015-08-25 13:17:39 -04002188 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002189 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002190 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002191 }
2192 }
2193
Geoff Lang7dd2e102014-11-10 15:19:26 -05002194 return true;
2195}
2196
Martin Radev4c4c8e72016-08-04 12:25:34 +03002197bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2198 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2199 const std::string &errorMessage,
2200 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002201{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002202 GLuint blockCount = 0;
2203 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002204 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002205 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002206 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002207 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002208 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002209 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002210 return false;
2211 }
2212 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002213 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002214 return true;
2215}
Jamie Madille473dee2015-08-18 14:49:01 -04002216
Martin Radev4c4c8e72016-08-04 12:25:34 +03002217bool Program::validateVertexAndFragmentInterfaceBlocks(
2218 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2219 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2220 InfoLog &infoLog) const
2221{
2222 // Check that interface blocks defined in the vertex and fragment shaders are identical
2223 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2224 UniformBlockMap linkedUniformBlocks;
2225
2226 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2227 {
2228 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2229 }
2230
Jamie Madille473dee2015-08-18 14:49:01 -04002231 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002232 {
Jamie Madille473dee2015-08-18 14:49:01 -04002233 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002234 if (entry != linkedUniformBlocks.end())
2235 {
2236 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2237 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2238 {
2239 return false;
2240 }
2241 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002242 }
2243 return true;
2244}
Jamie Madille473dee2015-08-18 14:49:01 -04002245
Martin Radev4c4c8e72016-08-04 12:25:34 +03002246bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2247{
2248 if (mState.mAttachedComputeShader)
2249 {
2250 const Shader &computeShader = *mState.mAttachedComputeShader;
2251 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2252
2253 if (!validateUniformBlocksCount(
2254 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2255 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2256 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002257 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002258 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002259 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002260 return true;
2261 }
2262
2263 const Shader &vertexShader = *mState.mAttachedVertexShader;
2264 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2265
2266 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2267 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2268
2269 if (!validateUniformBlocksCount(
2270 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2271 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2272 {
2273 return false;
2274 }
2275 if (!validateUniformBlocksCount(
2276 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2277 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2278 infoLog))
2279 {
2280
2281 return false;
2282 }
2283 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2284 infoLog))
2285 {
2286 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002287 }
Jamie Madille473dee2015-08-18 14:49:01 -04002288
Geoff Lang7dd2e102014-11-10 15:19:26 -05002289 return true;
2290}
2291
Jamie Madilla2c74982016-12-12 11:20:42 -05002292bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002293 const sh::InterfaceBlock &vertexInterfaceBlock,
2294 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002295{
2296 const char* blockName = vertexInterfaceBlock.name.c_str();
2297 // validate blocks for the same member types
2298 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2299 {
Jamie Madillf6113162015-05-07 11:49:21 -04002300 infoLog << "Types for interface block '" << blockName
2301 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002302 return false;
2303 }
2304 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2305 {
Jamie Madillf6113162015-05-07 11:49:21 -04002306 infoLog << "Array sizes differ for interface block '" << blockName
2307 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002308 return false;
2309 }
2310 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2311 {
Jamie Madillf6113162015-05-07 11:49:21 -04002312 infoLog << "Layout qualifiers differ for interface block '" << blockName
2313 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002314 return false;
2315 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002316 const unsigned int numBlockMembers =
2317 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002318 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2319 {
2320 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2321 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2322 if (vertexMember.name != fragmentMember.name)
2323 {
Jamie Madillf6113162015-05-07 11:49:21 -04002324 infoLog << "Name mismatch for field " << blockMemberIndex
2325 << " of interface block '" << blockName
2326 << "': (in vertex: '" << vertexMember.name
2327 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002328 return false;
2329 }
2330 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2331 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2332 {
2333 return false;
2334 }
2335 }
2336 return true;
2337}
2338
2339bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2340 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2341{
2342 if (vertexVariable.type != fragmentVariable.type)
2343 {
Jamie Madillf6113162015-05-07 11:49:21 -04002344 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002345 return false;
2346 }
2347 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2348 {
Jamie Madillf6113162015-05-07 11:49:21 -04002349 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002350 return false;
2351 }
2352 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2353 {
Jamie Madillf6113162015-05-07 11:49:21 -04002354 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002355 return false;
2356 }
2357
2358 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2359 {
Jamie Madillf6113162015-05-07 11:49:21 -04002360 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002361 return false;
2362 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002363 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002364 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2365 {
2366 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2367 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2368
2369 if (vertexMember.name != fragmentMember.name)
2370 {
Jamie Madillf6113162015-05-07 11:49:21 -04002371 infoLog << "Name mismatch for field '" << memberIndex
2372 << "' of " << variableName
2373 << ": (in vertex: '" << vertexMember.name
2374 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002375 return false;
2376 }
2377
2378 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2379 vertexMember.name + "'";
2380
2381 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2382 {
2383 return false;
2384 }
2385 }
2386
2387 return true;
2388}
2389
2390bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2391{
Cooper Partin1acf4382015-06-12 12:38:57 -07002392#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2393 const bool validatePrecision = true;
2394#else
2395 const bool validatePrecision = false;
2396#endif
2397
2398 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002399 {
2400 return false;
2401 }
2402
2403 return true;
2404}
2405
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002406bool Program::linkValidateVaryings(InfoLog &infoLog,
2407 const std::string &varyingName,
2408 const sh::Varying &vertexVarying,
2409 const sh::Varying &fragmentVarying,
2410 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002411{
2412 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2413 {
2414 return false;
2415 }
2416
Jamie Madille9cc4692015-02-19 16:00:13 -05002417 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002418 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002419 infoLog << "Interpolation types for " << varyingName
2420 << " differ between vertex and fragment shaders.";
2421 return false;
2422 }
2423
2424 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2425 {
2426 infoLog << "Invariance for " << varyingName
2427 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002428 return false;
2429 }
2430
2431 return true;
2432}
2433
Jamie Madillccdf74b2015-08-18 10:46:12 -04002434bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002435 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002436 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002437{
2438 size_t totalComponents = 0;
2439
Jamie Madillccdf74b2015-08-18 10:46:12 -04002440 std::set<std::string> uniqueNames;
2441
Jamie Madill48ef11b2016-04-27 15:21:52 -04002442 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002443 {
2444 bool found = false;
Jamie Madill192745a2016-12-22 15:58:21 -05002445 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002446 {
Jamie Madill192745a2016-12-22 15:58:21 -05002447 const sh::Varying *varying = ref.second.get();
2448
Jamie Madillccdf74b2015-08-18 10:46:12 -04002449 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002450 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002451 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002452 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002453 infoLog << "Two transform feedback varyings specify the same output variable ("
2454 << tfVaryingName << ").";
2455 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002456 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002457 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002458
Geoff Lang1a683462015-09-29 15:09:59 -04002459 if (varying->isArray())
2460 {
2461 infoLog << "Capture of arrays is undefined and not supported.";
2462 return false;
2463 }
2464
Jamie Madillccdf74b2015-08-18 10:46:12 -04002465 // TODO(jmadill): Investigate implementation limits on D3D11
Jamie Madilla2c74982016-12-12 11:20:42 -05002466 size_t componentCount = VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002467 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002468 componentCount > caps.maxTransformFeedbackSeparateComponents)
2469 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002470 infoLog << "Transform feedback varying's " << varying->name << " components ("
2471 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002472 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002473 return false;
2474 }
2475
2476 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002477 found = true;
2478 break;
2479 }
2480 }
2481
Jamie Madill89bb70e2015-08-31 14:18:39 -04002482 if (tfVaryingName.find('[') != std::string::npos)
2483 {
Geoff Lang1a683462015-09-29 15:09:59 -04002484 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002485 return false;
2486 }
2487
Geoff Lang7dd2e102014-11-10 15:19:26 -05002488 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2489 ASSERT(found);
2490 }
2491
Jamie Madill48ef11b2016-04-27 15:21:52 -04002492 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002493 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002494 {
Jamie Madillf6113162015-05-07 11:49:21 -04002495 infoLog << "Transform feedback varying total components (" << totalComponents
2496 << ") exceed the maximum interleaved components ("
2497 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002498 return false;
2499 }
2500
2501 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002502}
2503
Jamie Madill192745a2016-12-22 15:58:21 -05002504void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002505{
2506 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002507 mState.mTransformFeedbackVaryingVars.clear();
2508 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002509 {
Jamie Madill192745a2016-12-22 15:58:21 -05002510 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002511 {
Jamie Madill192745a2016-12-22 15:58:21 -05002512 const sh::Varying *varying = ref.second.get();
Jamie Madillccdf74b2015-08-18 10:46:12 -04002513 if (tfVaryingName == varying->name)
2514 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002515 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002516 break;
2517 }
2518 }
2519 }
2520}
2521
Jamie Madill192745a2016-12-22 15:58:21 -05002522Program::MergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002523{
Jamie Madill192745a2016-12-22 15:58:21 -05002524 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002525
Jamie Madill48ef11b2016-04-27 15:21:52 -04002526 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002527 {
Jamie Madill192745a2016-12-22 15:58:21 -05002528 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002529 }
2530
Jamie Madill48ef11b2016-04-27 15:21:52 -04002531 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002532 {
Jamie Madill192745a2016-12-22 15:58:21 -05002533 merged[varying.name].fragment = &varying;
2534 }
2535
2536 return merged;
2537}
2538
2539std::vector<PackedVarying> Program::getPackedVaryings(
2540 const Program::MergedVaryings &mergedVaryings) const
2541{
2542 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2543 std::vector<PackedVarying> packedVaryings;
2544
2545 for (const auto &ref : mergedVaryings)
2546 {
2547 const sh::Varying *input = ref.second.vertex;
2548 const sh::Varying *output = ref.second.fragment;
2549
2550 // Only pack varyings that have a matched input or output, plus special builtins.
2551 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002552 {
Jamie Madill192745a2016-12-22 15:58:21 -05002553 // Will get the vertex shader interpolation by default.
2554 auto interpolation = ref.second.get()->interpolation;
2555
2556 // Interpolation qualifiers must match.
2557 if (output->isStruct())
2558 {
2559 ASSERT(!output->isArray());
2560 for (const auto &field : output->fields)
2561 {
2562 ASSERT(!field.isStruct() && !field.isArray());
2563 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2564 }
2565 }
2566 else
2567 {
2568 packedVaryings.push_back(PackedVarying(*output, interpolation));
2569 }
2570 continue;
2571 }
2572
2573 // Keep Transform FB varyings in the merged list always.
2574 if (!input)
2575 {
2576 continue;
2577 }
2578
2579 for (const std::string &tfVarying : tfVaryings)
2580 {
2581 if (tfVarying == input->name)
2582 {
2583 // Transform feedback for varying structs is underspecified.
2584 // See Khronos bug 9856.
2585 // TODO(jmadill): Figure out how to be spec-compliant here.
2586 if (!input->isStruct())
2587 {
2588 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2589 packedVaryings.back().vertexOnly = true;
2590 }
2591 break;
2592 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002593 }
2594 }
2595
Jamie Madill192745a2016-12-22 15:58:21 -05002596 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2597
2598 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002599}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002600
2601void Program::linkOutputVariables()
2602{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002603 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002604 ASSERT(fragmentShader != nullptr);
2605
2606 // Skip this step for GLES2 shaders.
2607 if (fragmentShader->getShaderVersion() == 100)
2608 return;
2609
Jamie Madilla0a9e122015-09-02 15:54:30 -04002610 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002611
2612 // TODO(jmadill): any caps validation here?
2613
2614 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2615 outputVariableIndex++)
2616 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002617 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002618
2619 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2620 if (outputVariable.isBuiltIn())
2621 continue;
2622
2623 // Since multiple output locations must be specified, use 0 for non-specified locations.
2624 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2625
2626 ASSERT(outputVariable.staticUse);
2627
2628 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2629 elementIndex++)
2630 {
2631 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002632 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002633 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002634 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002635 VariableLocation(outputVariable.name, element, outputVariableIndex);
2636 }
2637 }
2638}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002639
Jamie Madilla2c74982016-12-12 11:20:42 -05002640bool Program::flattenUniformsAndCheckCapsForShader(const Shader &shader,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002641 GLuint maxUniformComponents,
2642 GLuint maxTextureImageUnits,
2643 const std::string &componentsErrorMessage,
2644 const std::string &samplerErrorMessage,
2645 std::vector<LinkedUniform> &samplerUniforms,
2646 InfoLog &infoLog)
2647{
2648 VectorAndSamplerCount vasCount;
2649 for (const sh::Uniform &uniform : shader.getUniforms())
2650 {
2651 if (uniform.staticUse)
2652 {
2653 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2654 }
2655 }
2656
2657 if (vasCount.vectorCount > maxUniformComponents)
2658 {
2659 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2660 return false;
2661 }
2662
2663 if (vasCount.samplerCount > maxTextureImageUnits)
2664 {
2665 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2666 return false;
2667 }
2668
2669 return true;
2670}
2671
Jamie Madill62d31cb2015-09-11 13:25:51 -04002672bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2673{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002674 std::vector<LinkedUniform> samplerUniforms;
2675
Martin Radev4c4c8e72016-08-04 12:25:34 +03002676 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002677 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002678 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002679
2680 // TODO (mradev): check whether we need finer-grained component counting
2681 if (!flattenUniformsAndCheckCapsForShader(
2682 *computeShader, caps.maxComputeUniformComponents / 4,
2683 caps.maxComputeTextureImageUnits,
2684 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2685 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2686 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002687 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002688 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002689 }
2690 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002691 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002692 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002693 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002694
Martin Radev4c4c8e72016-08-04 12:25:34 +03002695 if (!flattenUniformsAndCheckCapsForShader(
2696 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2697 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2698 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2699 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002700 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002701 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002702 }
Jamie Madilla2c74982016-12-12 11:20:42 -05002703 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002704
Martin Radev4c4c8e72016-08-04 12:25:34 +03002705 if (!flattenUniformsAndCheckCapsForShader(
2706 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2707 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2708 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2709 infoLog))
2710 {
2711 return false;
2712 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002713 }
2714
Jamie Madille7d84322017-01-10 18:21:59 -05002715 mState.mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
2716 mState.mSamplerUniformRange.end =
2717 mState.mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002718
Jamie Madill48ef11b2016-04-27 15:21:52 -04002719 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002720
Jamie Madille7d84322017-01-10 18:21:59 -05002721 // If uniform is a sampler type, insert it into the mSamplerBindings array.
2722 for (const auto &samplerUniform : samplerUniforms)
2723 {
2724 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2725 mState.mSamplerBindings.emplace_back(
2726 SamplerBinding(textureType, samplerUniform.elementCount()));
2727 }
2728
Jamie Madill62d31cb2015-09-11 13:25:51 -04002729 return true;
2730}
2731
2732Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002733 const std::string &fullName,
2734 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002735{
2736 VectorAndSamplerCount vectorAndSamplerCount;
2737
2738 if (uniform.isStruct())
2739 {
2740 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2741 {
2742 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2743
2744 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2745 {
2746 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2747 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2748
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002749 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002750 }
2751 }
2752
2753 return vectorAndSamplerCount;
2754 }
2755
2756 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002757 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002758 if (!UniformInList(mState.getUniforms(), fullName) &&
2759 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002760 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002761 LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
2762 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Jamie Madill62d31cb2015-09-11 13:25:51 -04002763 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002764
2765 // Store sampler uniforms separately, so we'll append them to the end of the list.
2766 if (isSampler)
2767 {
2768 samplerUniforms->push_back(linkedUniform);
2769 }
2770 else
2771 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002772 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002773 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002774 }
2775
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002776 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002777
2778 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2779 // Likewise, don't count "real" uniforms towards sampler count.
2780 vectorAndSamplerCount.vectorCount =
2781 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002782 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002783
2784 return vectorAndSamplerCount;
2785}
2786
2787void Program::gatherInterfaceBlockInfo()
2788{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002789 ASSERT(mState.mUniformBlocks.empty());
2790
2791 if (mState.mAttachedComputeShader)
2792 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002793 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002794
2795 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2796 {
2797
2798 // Only 'packed' blocks are allowed to be considered inactive.
2799 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2800 continue;
2801
Jamie Madilla2c74982016-12-12 11:20:42 -05002802 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002803 {
2804 if (block.name == computeBlock.name)
2805 {
2806 block.computeStaticUse = computeBlock.staticUse;
2807 }
2808 }
2809
2810 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2811 }
2812 return;
2813 }
2814
Jamie Madill62d31cb2015-09-11 13:25:51 -04002815 std::set<std::string> visitedList;
2816
Jamie Madilla2c74982016-12-12 11:20:42 -05002817 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002818
Jamie Madill62d31cb2015-09-11 13:25:51 -04002819 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2820 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002821 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002822 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2823 continue;
2824
2825 if (visitedList.count(vertexBlock.name) > 0)
2826 continue;
2827
2828 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2829 visitedList.insert(vertexBlock.name);
2830 }
2831
Jamie Madilla2c74982016-12-12 11:20:42 -05002832 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002833
2834 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2835 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002836 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002837 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2838 continue;
2839
2840 if (visitedList.count(fragmentBlock.name) > 0)
2841 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002842 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002843 {
2844 if (block.name == fragmentBlock.name)
2845 {
2846 block.fragmentStaticUse = fragmentBlock.staticUse;
2847 }
2848 }
2849
2850 continue;
2851 }
2852
2853 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2854 visitedList.insert(fragmentBlock.name);
2855 }
2856}
2857
Jamie Madill4a3c2342015-10-08 12:58:45 -04002858template <typename VarT>
2859void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2860 const std::string &prefix,
2861 int blockIndex)
2862{
2863 for (const VarT &field : fields)
2864 {
2865 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2866
2867 if (field.isStruct())
2868 {
2869 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2870 {
2871 const std::string uniformElementName =
2872 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2873 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2874 }
2875 }
2876 else
2877 {
2878 // If getBlockMemberInfo returns false, the uniform is optimized out.
2879 sh::BlockMemberInfo memberInfo;
2880 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2881 {
2882 continue;
2883 }
2884
2885 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2886 blockIndex, memberInfo);
2887
2888 // Since block uniforms have no location, we don't need to store them in the uniform
2889 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002890 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002891 }
2892 }
2893}
2894
Jamie Madill62d31cb2015-09-11 13:25:51 -04002895void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2896{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002897 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002898 size_t blockSize = 0;
2899
2900 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002901 std::stringstream blockNameStr;
2902 blockNameStr << interfaceBlock.name;
2903 if (interfaceBlock.arraySize > 0)
2904 {
2905 blockNameStr << "[0]";
2906 }
2907 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002908 {
2909 return;
2910 }
2911
2912 // Track the first and last uniform index to determine the range of active uniforms in the
2913 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002914 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002915 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002916 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002917
2918 std::vector<unsigned int> blockUniformIndexes;
2919 for (size_t blockUniformIndex = firstBlockUniformIndex;
2920 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2921 {
2922 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2923 }
2924
2925 if (interfaceBlock.arraySize > 0)
2926 {
2927 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2928 {
2929 UniformBlock block(interfaceBlock.name, true, arrayElement);
2930 block.memberUniformIndexes = blockUniformIndexes;
2931
Martin Radev4c4c8e72016-08-04 12:25:34 +03002932 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002933 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002934 case GL_VERTEX_SHADER:
2935 {
2936 block.vertexStaticUse = interfaceBlock.staticUse;
2937 break;
2938 }
2939 case GL_FRAGMENT_SHADER:
2940 {
2941 block.fragmentStaticUse = interfaceBlock.staticUse;
2942 break;
2943 }
2944 case GL_COMPUTE_SHADER:
2945 {
2946 block.computeStaticUse = interfaceBlock.staticUse;
2947 break;
2948 }
2949 default:
2950 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002951 }
2952
Qin Jiajia0350a642016-11-01 17:01:51 +08002953 // Since all block elements in an array share the same active uniforms, they will all be
2954 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2955 // here we will add every block element in the array.
2956 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002957 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002958 }
2959 }
2960 else
2961 {
2962 UniformBlock block(interfaceBlock.name, false, 0);
2963 block.memberUniformIndexes = blockUniformIndexes;
2964
Martin Radev4c4c8e72016-08-04 12:25:34 +03002965 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002966 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002967 case GL_VERTEX_SHADER:
2968 {
2969 block.vertexStaticUse = interfaceBlock.staticUse;
2970 break;
2971 }
2972 case GL_FRAGMENT_SHADER:
2973 {
2974 block.fragmentStaticUse = interfaceBlock.staticUse;
2975 break;
2976 }
2977 case GL_COMPUTE_SHADER:
2978 {
2979 block.computeStaticUse = interfaceBlock.staticUse;
2980 break;
2981 }
2982 default:
2983 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002984 }
2985
Jamie Madill4a3c2342015-10-08 12:58:45 -04002986 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002987 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002988 }
2989}
2990
Jamie Madille7d84322017-01-10 18:21:59 -05002991template <>
2992void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2993 const uint8_t *destPointer,
2994 GLsizei clampedCount,
2995 const GLint *v)
2996{
2997 // Invalidate the validation cache only if we modify the sampler data.
2998 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2999 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
3000 {
3001 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3002 std::vector<GLuint> *boundTextureUnits =
3003 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
3004
3005 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
3006 mCachedValidateSamplersResult.reset();
3007 }
3008}
3009
3010template <typename T>
3011void Program::updateSamplerUniform(const VariableLocation &locationInfo,
3012 const uint8_t *destPointer,
3013 GLsizei clampedCount,
3014 const T *v)
3015{
3016}
3017
Jamie Madill62d31cb2015-09-11 13:25:51 -04003018template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003019GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003020{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003021 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3022 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003023 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
3024
Corentin Wallez15ac5342016-11-03 17:06:39 -04003025 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3026 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3027 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003028 GLsizei maxElementCount =
3029 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
3030
3031 GLsizei count = countIn;
3032 GLsizei clampedCount = count * vectorSize;
3033 if (clampedCount > maxElementCount)
3034 {
3035 clampedCount = maxElementCount;
3036 count = maxElementCount / vectorSize;
3037 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04003038
Jamie Madill62d31cb2015-09-11 13:25:51 -04003039 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
3040 {
3041 // Do a cast conversion for boolean types. From the spec:
3042 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
3043 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003044 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003045 {
3046 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
3047 }
3048 }
3049 else
3050 {
Jamie Madille7d84322017-01-10 18:21:59 -05003051 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003052 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003053 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003054
3055 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003056}
3057
3058template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003059GLsizei Program::setMatrixUniformInternal(GLint location,
3060 GLsizei count,
3061 GLboolean transpose,
3062 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003063{
3064 if (!transpose)
3065 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003066 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003067 }
3068
3069 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003070 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3071 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003072 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04003073
3074 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3075 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3076 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
3077 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
3078
3079 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003080 {
3081 size_t elementOffset = element * rows * cols;
3082
3083 for (size_t row = 0; row < rows; ++row)
3084 {
3085 for (size_t col = 0; col < cols; ++col)
3086 {
3087 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
3088 }
3089 }
3090 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003091
3092 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003093}
3094
3095template <typename DestT>
3096void Program::getUniformInternal(GLint location, DestT *dataOut) const
3097{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003098 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3099 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003100
3101 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
3102
3103 GLenum componentType = VariableComponentType(uniform.type);
3104 if (componentType == GLTypeToGLenum<DestT>::value)
3105 {
3106 memcpy(dataOut, srcPointer, uniform.getElementSize());
3107 return;
3108 }
3109
Corentin Wallez6596c462016-03-17 17:26:58 -04003110 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003111
3112 switch (componentType)
3113 {
3114 case GL_INT:
3115 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
3116 break;
3117 case GL_UNSIGNED_INT:
3118 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
3119 break;
3120 case GL_BOOL:
3121 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
3122 break;
3123 case GL_FLOAT:
3124 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
3125 break;
3126 default:
3127 UNREACHABLE();
3128 }
3129}
Jamie Madilla4595b82017-01-11 17:36:34 -05003130
3131bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3132{
3133 // Must be called after samplers are validated.
3134 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3135
3136 for (const auto &binding : mState.mSamplerBindings)
3137 {
3138 GLenum textureType = binding.textureType;
3139 for (const auto &unit : binding.boundTextureUnits)
3140 {
3141 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3142 if (programTextureID == textureID)
3143 {
3144 // TODO(jmadill): Check for appropriate overlap.
3145 return true;
3146 }
3147 }
3148 }
3149
3150 return false;
3151}
3152
Jamie Madilla2c74982016-12-12 11:20:42 -05003153} // namespace gl