blob: 6fd31a0384fe7e2236cab944e039bc1e6020217d [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
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000513 mUniformLocationBindings.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
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000640 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300641 {
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
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000688 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300689 {
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
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001954bool Program::linkUniforms(InfoLog &infoLog,
1955 const Caps &caps,
1956 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001957{
1958 if (mState.mAttachedVertexShader && mState.mAttachedFragmentShader)
1959 {
1960 ASSERT(mState.mAttachedComputeShader == nullptr);
1961 if (!validateVertexAndFragmentUniforms(infoLog))
1962 {
1963 return false;
1964 }
1965 }
Jamie Madillea918db2015-08-18 14:48:59 -04001966
Jamie Madill62d31cb2015-09-11 13:25:51 -04001967 // Flatten the uniforms list (nested fields) into a simple list (no nesting).
1968 // Also check the maximum uniform vector and sampler counts.
1969 if (!flattenUniformsAndCheckCaps(caps, infoLog))
1970 {
1971 return false;
1972 }
1973
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001974 if (!indexUniforms(infoLog, caps, uniformLocationBindings))
Geoff Langd8605522016-04-13 10:19:12 -04001975 {
1976 return false;
1977 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04001978
Jamie Madillea918db2015-08-18 14:48:59 -04001979 return true;
1980}
1981
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001982bool Program::indexUniforms(InfoLog &infoLog,
1983 const Caps &caps,
1984 const Bindings &uniformLocationBindings)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001985{
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001986 std::vector<VariableLocation> unlocatedUniforms;
1987 std::map<GLuint, VariableLocation> preLocatedUniforms;
Geoff Langd8605522016-04-13 10:19:12 -04001988 int maxUniformLocation = -1;
1989
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001990 // Gather uniforms that have their location pre-set and uniforms that don't yet have a location.
Jamie Madill48ef11b2016-04-27 15:21:52 -04001991 for (size_t uniformIndex = 0; uniformIndex < mState.mUniforms.size(); uniformIndex++)
Jamie Madill62d31cb2015-09-11 13:25:51 -04001992 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001993 const LinkedUniform &uniform = mState.mUniforms[uniformIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001994
Geoff Langd8605522016-04-13 10:19:12 -04001995 if (uniform.isBuiltIn())
1996 {
1997 continue;
1998 }
1999
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002000 int preSetLocation = uniformLocationBindings.getBinding(uniform.name);
Geoff Langd8605522016-04-13 10:19:12 -04002001
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002002 // Verify that this location isn't used twice
2003 if (preSetLocation != -1 &&
2004 preLocatedUniforms.find(preSetLocation) != preLocatedUniforms.end())
Geoff Langd8605522016-04-13 10:19:12 -04002005 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002006 infoLog << "Multiple uniforms bound to location " << preSetLocation << ".";
Geoff Langd8605522016-04-13 10:19:12 -04002007 return false;
2008 }
2009
Jamie Madill62d31cb2015-09-11 13:25:51 -04002010 for (unsigned int arrayIndex = 0; arrayIndex < uniform.elementCount(); arrayIndex++)
2011 {
Geoff Langd8605522016-04-13 10:19:12 -04002012 VariableLocation location(uniform.name, arrayIndex,
2013 static_cast<unsigned int>(uniformIndex));
2014
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002015 if (arrayIndex == 0 && preSetLocation != -1)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002016 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002017 preLocatedUniforms[preSetLocation] = location;
2018 maxUniformLocation = std::max(maxUniformLocation, preSetLocation);
Geoff Langd8605522016-04-13 10:19:12 -04002019 }
2020 else
2021 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002022 unlocatedUniforms.push_back(location);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002023 }
2024 }
2025 }
Geoff Langd8605522016-04-13 10:19:12 -04002026
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002027 // Gather the reserved locations, ones that are bound but not referenced. Other uniforms should
Geoff Langd8605522016-04-13 10:19:12 -04002028 // not be assigned to those locations.
2029 std::set<GLuint> reservedLocations;
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002030 for (const auto &locationBinding : uniformLocationBindings)
Geoff Langd8605522016-04-13 10:19:12 -04002031 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002032 GLuint location = locationBinding.second;
2033 if (preLocatedUniforms.find(location) == preLocatedUniforms.end())
Geoff Langd8605522016-04-13 10:19:12 -04002034 {
2035 reservedLocations.insert(location);
2036 maxUniformLocation = std::max(maxUniformLocation, static_cast<int>(location));
2037 }
2038 }
2039
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002040 // Make enough space for all uniforms, with pre-set locations or not.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002041 mState.mUniformLocations.resize(
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002042 std::max(unlocatedUniforms.size() + preLocatedUniforms.size() + reservedLocations.size(),
Geoff Langd8605522016-04-13 10:19:12 -04002043 static_cast<size_t>(maxUniformLocation + 1)));
2044
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002045 // Assign uniforms with pre-set locations
2046 for (const auto &uniform : preLocatedUniforms)
Geoff Langd8605522016-04-13 10:19:12 -04002047 {
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002048 mState.mUniformLocations[uniform.first] = uniform.second;
Geoff Langd8605522016-04-13 10:19:12 -04002049 }
2050
2051 // Assign reserved uniforms
2052 for (const auto &reservedLocation : reservedLocations)
2053 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002054 mState.mUniformLocations[reservedLocation].ignored = true;
Geoff Langd8605522016-04-13 10:19:12 -04002055 }
2056
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002057 // Automatically assign locations for the rest of the uniforms
Geoff Langd8605522016-04-13 10:19:12 -04002058 size_t nextUniformLocation = 0;
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002059 for (const auto &unlocatedUniform : unlocatedUniforms)
Geoff Langd8605522016-04-13 10:19:12 -04002060 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002061 while (mState.mUniformLocations[nextUniformLocation].used ||
2062 mState.mUniformLocations[nextUniformLocation].ignored)
Geoff Langd8605522016-04-13 10:19:12 -04002063 {
2064 nextUniformLocation++;
2065 }
2066
Jamie Madill48ef11b2016-04-27 15:21:52 -04002067 ASSERT(nextUniformLocation < mState.mUniformLocations.size());
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00002068 mState.mUniformLocations[nextUniformLocation] = unlocatedUniform;
Geoff Langd8605522016-04-13 10:19:12 -04002069 nextUniformLocation++;
2070 }
2071
2072 return true;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002073}
2074
Martin Radev4c4c8e72016-08-04 12:25:34 +03002075bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2076 const std::string &uniformName,
2077 const sh::InterfaceBlockField &vertexUniform,
2078 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079{
Jamie Madillc4c744222015-11-04 09:39:47 -05002080 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2081 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082 {
2083 return false;
2084 }
2085
2086 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2087 {
Jamie Madillf6113162015-05-07 11:49:21 -04002088 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002089 return false;
2090 }
2091
2092 return true;
2093}
2094
Jamie Madilleb979bf2016-11-15 12:28:46 -05002095// Assigns locations to all attributes from the bindings and program locations.
2096bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002097{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002098 const auto *vertexShader = mState.getAttachedVertexShader();
2099
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002101 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002102 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002103
2104 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002105 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002106 {
Jamie Madillf6113162015-05-07 11:49:21 -04002107 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002108 return false;
2109 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002111 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002112
Jamie Madillc349ec02015-08-21 16:53:12 -04002113 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002114 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002115 {
2116 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05002117 ASSERT(attribute.staticUse);
2118
Jamie Madilleb979bf2016-11-15 12:28:46 -05002119 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002120 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002121 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002122 attribute.location = bindingLocation;
2123 }
2124
2125 if (attribute.location != -1)
2126 {
2127 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002128 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002129
Jamie Madill63805b42015-08-25 13:17:39 -04002130 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002131 {
Jamie Madillf6113162015-05-07 11:49:21 -04002132 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002133 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002134
2135 return false;
2136 }
2137
Jamie Madill63805b42015-08-25 13:17:39 -04002138 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002139 {
Jamie Madill63805b42015-08-25 13:17:39 -04002140 const int regLocation = attribute.location + reg;
2141 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142
2143 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002144 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002145 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002146 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002147 // TODO(jmadill): fix aliasing on ES2
2148 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002149 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002150 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002151 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002152 return false;
2153 }
2154 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002155 else
2156 {
Jamie Madill63805b42015-08-25 13:17:39 -04002157 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002158 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002159
Jamie Madill63805b42015-08-25 13:17:39 -04002160 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002161 }
2162 }
2163 }
2164
2165 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002166 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002167 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002168 ASSERT(attribute.staticUse);
2169
Jamie Madillc349ec02015-08-21 16:53:12 -04002170 // Not set by glBindAttribLocation or by location layout qualifier
2171 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002172 {
Jamie Madill63805b42015-08-25 13:17:39 -04002173 int regs = VariableRegisterCount(attribute.type);
2174 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175
Jamie Madill63805b42015-08-25 13:17:39 -04002176 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002177 {
Jamie Madillf6113162015-05-07 11:49:21 -04002178 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002179 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002180 }
2181
Jamie Madillc349ec02015-08-21 16:53:12 -04002182 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002183 }
2184 }
2185
Jamie Madill48ef11b2016-04-27 15:21:52 -04002186 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002187 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002188 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002189 ASSERT(attribute.location != -1);
2190 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002191
Jamie Madill63805b42015-08-25 13:17:39 -04002192 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002193 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002194 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002195 }
2196 }
2197
Geoff Lang7dd2e102014-11-10 15:19:26 -05002198 return true;
2199}
2200
Martin Radev4c4c8e72016-08-04 12:25:34 +03002201bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2202 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2203 const std::string &errorMessage,
2204 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002205{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002206 GLuint blockCount = 0;
2207 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002208 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002209 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002210 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002211 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002212 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002213 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002214 return false;
2215 }
2216 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002217 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002218 return true;
2219}
Jamie Madille473dee2015-08-18 14:49:01 -04002220
Martin Radev4c4c8e72016-08-04 12:25:34 +03002221bool Program::validateVertexAndFragmentInterfaceBlocks(
2222 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2223 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2224 InfoLog &infoLog) const
2225{
2226 // Check that interface blocks defined in the vertex and fragment shaders are identical
2227 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2228 UniformBlockMap linkedUniformBlocks;
2229
2230 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2231 {
2232 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2233 }
2234
Jamie Madille473dee2015-08-18 14:49:01 -04002235 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002236 {
Jamie Madille473dee2015-08-18 14:49:01 -04002237 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002238 if (entry != linkedUniformBlocks.end())
2239 {
2240 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2241 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2242 {
2243 return false;
2244 }
2245 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002246 }
2247 return true;
2248}
Jamie Madille473dee2015-08-18 14:49:01 -04002249
Martin Radev4c4c8e72016-08-04 12:25:34 +03002250bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2251{
2252 if (mState.mAttachedComputeShader)
2253 {
2254 const Shader &computeShader = *mState.mAttachedComputeShader;
2255 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2256
2257 if (!validateUniformBlocksCount(
2258 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2259 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2260 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002261 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002262 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002263 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002264 return true;
2265 }
2266
2267 const Shader &vertexShader = *mState.mAttachedVertexShader;
2268 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2269
2270 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2271 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2272
2273 if (!validateUniformBlocksCount(
2274 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2275 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2276 {
2277 return false;
2278 }
2279 if (!validateUniformBlocksCount(
2280 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2281 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2282 infoLog))
2283 {
2284
2285 return false;
2286 }
2287 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2288 infoLog))
2289 {
2290 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002291 }
Jamie Madille473dee2015-08-18 14:49:01 -04002292
Geoff Lang7dd2e102014-11-10 15:19:26 -05002293 return true;
2294}
2295
Jamie Madilla2c74982016-12-12 11:20:42 -05002296bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002297 const sh::InterfaceBlock &vertexInterfaceBlock,
2298 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002299{
2300 const char* blockName = vertexInterfaceBlock.name.c_str();
2301 // validate blocks for the same member types
2302 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2303 {
Jamie Madillf6113162015-05-07 11:49:21 -04002304 infoLog << "Types for interface block '" << blockName
2305 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002306 return false;
2307 }
2308 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2309 {
Jamie Madillf6113162015-05-07 11:49:21 -04002310 infoLog << "Array sizes differ for interface block '" << blockName
2311 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002312 return false;
2313 }
2314 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2315 {
Jamie Madillf6113162015-05-07 11:49:21 -04002316 infoLog << "Layout qualifiers differ for interface block '" << blockName
2317 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002318 return false;
2319 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002320 const unsigned int numBlockMembers =
2321 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002322 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2323 {
2324 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2325 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2326 if (vertexMember.name != fragmentMember.name)
2327 {
Jamie Madillf6113162015-05-07 11:49:21 -04002328 infoLog << "Name mismatch for field " << blockMemberIndex
2329 << " of interface block '" << blockName
2330 << "': (in vertex: '" << vertexMember.name
2331 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002332 return false;
2333 }
2334 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2335 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2336 {
2337 return false;
2338 }
2339 }
2340 return true;
2341}
2342
2343bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2344 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2345{
2346 if (vertexVariable.type != fragmentVariable.type)
2347 {
Jamie Madillf6113162015-05-07 11:49:21 -04002348 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002349 return false;
2350 }
2351 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2352 {
Jamie Madillf6113162015-05-07 11:49:21 -04002353 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002354 return false;
2355 }
2356 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2357 {
Jamie Madillf6113162015-05-07 11:49:21 -04002358 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002359 return false;
2360 }
2361
2362 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2363 {
Jamie Madillf6113162015-05-07 11:49:21 -04002364 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002365 return false;
2366 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002367 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002368 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2369 {
2370 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2371 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2372
2373 if (vertexMember.name != fragmentMember.name)
2374 {
Jamie Madillf6113162015-05-07 11:49:21 -04002375 infoLog << "Name mismatch for field '" << memberIndex
2376 << "' of " << variableName
2377 << ": (in vertex: '" << vertexMember.name
2378 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002379 return false;
2380 }
2381
2382 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2383 vertexMember.name + "'";
2384
2385 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2386 {
2387 return false;
2388 }
2389 }
2390
2391 return true;
2392}
2393
2394bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
2395{
Cooper Partin1acf4382015-06-12 12:38:57 -07002396#if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED
2397 const bool validatePrecision = true;
2398#else
2399 const bool validatePrecision = false;
2400#endif
2401
2402 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002403 {
2404 return false;
2405 }
2406
2407 return true;
2408}
2409
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002410bool Program::linkValidateVaryings(InfoLog &infoLog,
2411 const std::string &varyingName,
2412 const sh::Varying &vertexVarying,
2413 const sh::Varying &fragmentVarying,
2414 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002415{
2416 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2417 {
2418 return false;
2419 }
2420
Jamie Madille9cc4692015-02-19 16:00:13 -05002421 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002422 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002423 infoLog << "Interpolation types for " << varyingName
2424 << " differ between vertex and fragment shaders.";
2425 return false;
2426 }
2427
2428 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2429 {
2430 infoLog << "Invariance for " << varyingName
2431 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002432 return false;
2433 }
2434
2435 return true;
2436}
2437
Jamie Madillccdf74b2015-08-18 10:46:12 -04002438bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002439 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002440 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002441{
2442 size_t totalComponents = 0;
2443
Jamie Madillccdf74b2015-08-18 10:46:12 -04002444 std::set<std::string> uniqueNames;
2445
Jamie Madill48ef11b2016-04-27 15:21:52 -04002446 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002447 {
2448 bool found = false;
Jamie Madill192745a2016-12-22 15:58:21 -05002449 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002450 {
Jamie Madill192745a2016-12-22 15:58:21 -05002451 const sh::Varying *varying = ref.second.get();
2452
Jamie Madillccdf74b2015-08-18 10:46:12 -04002453 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002454 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002455 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002456 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002457 infoLog << "Two transform feedback varyings specify the same output variable ("
2458 << tfVaryingName << ").";
2459 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002460 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002461 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002462
Geoff Lang1a683462015-09-29 15:09:59 -04002463 if (varying->isArray())
2464 {
2465 infoLog << "Capture of arrays is undefined and not supported.";
2466 return false;
2467 }
2468
Jamie Madillccdf74b2015-08-18 10:46:12 -04002469 // TODO(jmadill): Investigate implementation limits on D3D11
Jamie Madilla2c74982016-12-12 11:20:42 -05002470 size_t componentCount = VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002471 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002472 componentCount > caps.maxTransformFeedbackSeparateComponents)
2473 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002474 infoLog << "Transform feedback varying's " << varying->name << " components ("
2475 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002476 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002477 return false;
2478 }
2479
2480 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002481 found = true;
2482 break;
2483 }
2484 }
2485
Jamie Madill89bb70e2015-08-31 14:18:39 -04002486 if (tfVaryingName.find('[') != std::string::npos)
2487 {
Geoff Lang1a683462015-09-29 15:09:59 -04002488 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002489 return false;
2490 }
2491
Geoff Lang7dd2e102014-11-10 15:19:26 -05002492 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2493 ASSERT(found);
2494 }
2495
Jamie Madill48ef11b2016-04-27 15:21:52 -04002496 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002497 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002498 {
Jamie Madillf6113162015-05-07 11:49:21 -04002499 infoLog << "Transform feedback varying total components (" << totalComponents
2500 << ") exceed the maximum interleaved components ("
2501 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002502 return false;
2503 }
2504
2505 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002506}
2507
Jamie Madill192745a2016-12-22 15:58:21 -05002508void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002509{
2510 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002511 mState.mTransformFeedbackVaryingVars.clear();
2512 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002513 {
Jamie Madill192745a2016-12-22 15:58:21 -05002514 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002515 {
Jamie Madill192745a2016-12-22 15:58:21 -05002516 const sh::Varying *varying = ref.second.get();
Jamie Madillccdf74b2015-08-18 10:46:12 -04002517 if (tfVaryingName == varying->name)
2518 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002519 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002520 break;
2521 }
2522 }
2523 }
2524}
2525
Jamie Madill192745a2016-12-22 15:58:21 -05002526Program::MergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002527{
Jamie Madill192745a2016-12-22 15:58:21 -05002528 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002529
Jamie Madill48ef11b2016-04-27 15:21:52 -04002530 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002531 {
Jamie Madill192745a2016-12-22 15:58:21 -05002532 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002533 }
2534
Jamie Madill48ef11b2016-04-27 15:21:52 -04002535 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002536 {
Jamie Madill192745a2016-12-22 15:58:21 -05002537 merged[varying.name].fragment = &varying;
2538 }
2539
2540 return merged;
2541}
2542
2543std::vector<PackedVarying> Program::getPackedVaryings(
2544 const Program::MergedVaryings &mergedVaryings) const
2545{
2546 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2547 std::vector<PackedVarying> packedVaryings;
2548
2549 for (const auto &ref : mergedVaryings)
2550 {
2551 const sh::Varying *input = ref.second.vertex;
2552 const sh::Varying *output = ref.second.fragment;
2553
2554 // Only pack varyings that have a matched input or output, plus special builtins.
2555 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002556 {
Jamie Madill192745a2016-12-22 15:58:21 -05002557 // Will get the vertex shader interpolation by default.
2558 auto interpolation = ref.second.get()->interpolation;
2559
2560 // Interpolation qualifiers must match.
2561 if (output->isStruct())
2562 {
2563 ASSERT(!output->isArray());
2564 for (const auto &field : output->fields)
2565 {
2566 ASSERT(!field.isStruct() && !field.isArray());
2567 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2568 }
2569 }
2570 else
2571 {
2572 packedVaryings.push_back(PackedVarying(*output, interpolation));
2573 }
2574 continue;
2575 }
2576
2577 // Keep Transform FB varyings in the merged list always.
2578 if (!input)
2579 {
2580 continue;
2581 }
2582
2583 for (const std::string &tfVarying : tfVaryings)
2584 {
2585 if (tfVarying == input->name)
2586 {
2587 // Transform feedback for varying structs is underspecified.
2588 // See Khronos bug 9856.
2589 // TODO(jmadill): Figure out how to be spec-compliant here.
2590 if (!input->isStruct())
2591 {
2592 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2593 packedVaryings.back().vertexOnly = true;
2594 }
2595 break;
2596 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002597 }
2598 }
2599
Jamie Madill192745a2016-12-22 15:58:21 -05002600 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2601
2602 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002603}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002604
2605void Program::linkOutputVariables()
2606{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002607 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002608 ASSERT(fragmentShader != nullptr);
2609
2610 // Skip this step for GLES2 shaders.
2611 if (fragmentShader->getShaderVersion() == 100)
2612 return;
2613
Jamie Madilla0a9e122015-09-02 15:54:30 -04002614 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002615
2616 // TODO(jmadill): any caps validation here?
2617
2618 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2619 outputVariableIndex++)
2620 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002621 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002622
2623 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2624 if (outputVariable.isBuiltIn())
2625 continue;
2626
2627 // Since multiple output locations must be specified, use 0 for non-specified locations.
2628 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2629
2630 ASSERT(outputVariable.staticUse);
2631
2632 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2633 elementIndex++)
2634 {
2635 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002636 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002637 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002638 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002639 VariableLocation(outputVariable.name, element, outputVariableIndex);
2640 }
2641 }
2642}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002643
Jamie Madilla2c74982016-12-12 11:20:42 -05002644bool Program::flattenUniformsAndCheckCapsForShader(const Shader &shader,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002645 GLuint maxUniformComponents,
2646 GLuint maxTextureImageUnits,
2647 const std::string &componentsErrorMessage,
2648 const std::string &samplerErrorMessage,
2649 std::vector<LinkedUniform> &samplerUniforms,
2650 InfoLog &infoLog)
2651{
2652 VectorAndSamplerCount vasCount;
2653 for (const sh::Uniform &uniform : shader.getUniforms())
2654 {
2655 if (uniform.staticUse)
2656 {
2657 vasCount += flattenUniform(uniform, uniform.name, &samplerUniforms);
2658 }
2659 }
2660
2661 if (vasCount.vectorCount > maxUniformComponents)
2662 {
2663 infoLog << componentsErrorMessage << maxUniformComponents << ").";
2664 return false;
2665 }
2666
2667 if (vasCount.samplerCount > maxTextureImageUnits)
2668 {
2669 infoLog << samplerErrorMessage << maxTextureImageUnits << ").";
2670 return false;
2671 }
2672
2673 return true;
2674}
2675
Jamie Madill62d31cb2015-09-11 13:25:51 -04002676bool Program::flattenUniformsAndCheckCaps(const Caps &caps, InfoLog &infoLog)
2677{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002678 std::vector<LinkedUniform> samplerUniforms;
2679
Martin Radev4c4c8e72016-08-04 12:25:34 +03002680 if (mState.mAttachedComputeShader)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002681 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002682 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002683
2684 // TODO (mradev): check whether we need finer-grained component counting
2685 if (!flattenUniformsAndCheckCapsForShader(
2686 *computeShader, caps.maxComputeUniformComponents / 4,
2687 caps.maxComputeTextureImageUnits,
2688 "Compute shader active uniforms exceed MAX_COMPUTE_UNIFORM_COMPONENTS (",
2689 "Compute shader sampler count exceeds MAX_COMPUTE_TEXTURE_IMAGE_UNITS (",
2690 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002691 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002692 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002693 }
2694 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002695 else
Jamie Madill62d31cb2015-09-11 13:25:51 -04002696 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002697 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002698
Martin Radev4c4c8e72016-08-04 12:25:34 +03002699 if (!flattenUniformsAndCheckCapsForShader(
2700 *vertexShader, caps.maxVertexUniformVectors, caps.maxVertexTextureImageUnits,
2701 "Vertex shader active uniforms exceed MAX_VERTEX_UNIFORM_VECTORS (",
2702 "Vertex shader sampler count exceeds MAX_VERTEX_TEXTURE_IMAGE_UNITS (",
2703 samplerUniforms, infoLog))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002704 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002705 return false;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002706 }
Jamie Madilla2c74982016-12-12 11:20:42 -05002707 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002708
Martin Radev4c4c8e72016-08-04 12:25:34 +03002709 if (!flattenUniformsAndCheckCapsForShader(
2710 *fragmentShader, caps.maxFragmentUniformVectors, caps.maxTextureImageUnits,
2711 "Fragment shader active uniforms exceed MAX_FRAGMENT_UNIFORM_VECTORS (",
2712 "Fragment shader sampler count exceeds MAX_TEXTURE_IMAGE_UNITS (", samplerUniforms,
2713 infoLog))
2714 {
2715 return false;
2716 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002717 }
2718
Jamie Madille7d84322017-01-10 18:21:59 -05002719 mState.mSamplerUniformRange.start = static_cast<unsigned int>(mState.mUniforms.size());
2720 mState.mSamplerUniformRange.end =
2721 mState.mSamplerUniformRange.start + static_cast<unsigned int>(samplerUniforms.size());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002722
Jamie Madill48ef11b2016-04-27 15:21:52 -04002723 mState.mUniforms.insert(mState.mUniforms.end(), samplerUniforms.begin(), samplerUniforms.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002724
Jamie Madille7d84322017-01-10 18:21:59 -05002725 // If uniform is a sampler type, insert it into the mSamplerBindings array.
2726 for (const auto &samplerUniform : samplerUniforms)
2727 {
2728 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2729 mState.mSamplerBindings.emplace_back(
2730 SamplerBinding(textureType, samplerUniform.elementCount()));
2731 }
2732
Jamie Madill62d31cb2015-09-11 13:25:51 -04002733 return true;
2734}
2735
2736Program::VectorAndSamplerCount Program::flattenUniform(const sh::ShaderVariable &uniform,
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002737 const std::string &fullName,
2738 std::vector<LinkedUniform> *samplerUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002739{
2740 VectorAndSamplerCount vectorAndSamplerCount;
2741
2742 if (uniform.isStruct())
2743 {
2744 for (unsigned int elementIndex = 0; elementIndex < uniform.elementCount(); elementIndex++)
2745 {
2746 const std::string &elementString = (uniform.isArray() ? ArrayString(elementIndex) : "");
2747
2748 for (size_t fieldIndex = 0; fieldIndex < uniform.fields.size(); fieldIndex++)
2749 {
2750 const sh::ShaderVariable &field = uniform.fields[fieldIndex];
2751 const std::string &fieldFullName = (fullName + elementString + "." + field.name);
2752
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002753 vectorAndSamplerCount += flattenUniform(field, fieldFullName, samplerUniforms);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002754 }
2755 }
2756
2757 return vectorAndSamplerCount;
2758 }
2759
2760 // Not a struct
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002761 bool isSampler = IsSamplerType(uniform.type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002762 if (!UniformInList(mState.getUniforms(), fullName) &&
2763 !UniformInList(*samplerUniforms, fullName))
Jamie Madill62d31cb2015-09-11 13:25:51 -04002764 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002765 LinkedUniform linkedUniform(uniform.type, uniform.precision, fullName, uniform.arraySize,
2766 -1, sh::BlockMemberInfo::getDefaultBlockInfo());
Jamie Madill62d31cb2015-09-11 13:25:51 -04002767 linkedUniform.staticUse = true;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002768
2769 // Store sampler uniforms separately, so we'll append them to the end of the list.
2770 if (isSampler)
2771 {
2772 samplerUniforms->push_back(linkedUniform);
2773 }
2774 else
2775 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002776 mState.mUniforms.push_back(linkedUniform);
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002777 }
Jamie Madill62d31cb2015-09-11 13:25:51 -04002778 }
2779
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002780 unsigned int elementCount = uniform.elementCount();
Austin Kinross7a3e8e22015-10-08 15:50:06 -07002781
2782 // Samplers aren't "real" uniforms, so they don't count towards register usage.
2783 // Likewise, don't count "real" uniforms towards sampler count.
2784 vectorAndSamplerCount.vectorCount =
2785 (isSampler ? 0 : (VariableRegisterCount(uniform.type) * elementCount));
Jamie Madill3d3d2f22015-09-23 16:47:51 -04002786 vectorAndSamplerCount.samplerCount = (isSampler ? elementCount : 0);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002787
2788 return vectorAndSamplerCount;
2789}
2790
2791void Program::gatherInterfaceBlockInfo()
2792{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002793 ASSERT(mState.mUniformBlocks.empty());
2794
2795 if (mState.mAttachedComputeShader)
2796 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002797 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002798
2799 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2800 {
2801
2802 // Only 'packed' blocks are allowed to be considered inactive.
2803 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2804 continue;
2805
Jamie Madilla2c74982016-12-12 11:20:42 -05002806 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002807 {
2808 if (block.name == computeBlock.name)
2809 {
2810 block.computeStaticUse = computeBlock.staticUse;
2811 }
2812 }
2813
2814 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2815 }
2816 return;
2817 }
2818
Jamie Madill62d31cb2015-09-11 13:25:51 -04002819 std::set<std::string> visitedList;
2820
Jamie Madilla2c74982016-12-12 11:20:42 -05002821 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002822
Jamie Madill62d31cb2015-09-11 13:25:51 -04002823 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2824 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002825 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002826 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2827 continue;
2828
2829 if (visitedList.count(vertexBlock.name) > 0)
2830 continue;
2831
2832 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2833 visitedList.insert(vertexBlock.name);
2834 }
2835
Jamie Madilla2c74982016-12-12 11:20:42 -05002836 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002837
2838 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2839 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002840 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002841 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2842 continue;
2843
2844 if (visitedList.count(fragmentBlock.name) > 0)
2845 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002846 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002847 {
2848 if (block.name == fragmentBlock.name)
2849 {
2850 block.fragmentStaticUse = fragmentBlock.staticUse;
2851 }
2852 }
2853
2854 continue;
2855 }
2856
2857 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2858 visitedList.insert(fragmentBlock.name);
2859 }
2860}
2861
Jamie Madill4a3c2342015-10-08 12:58:45 -04002862template <typename VarT>
2863void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2864 const std::string &prefix,
2865 int blockIndex)
2866{
2867 for (const VarT &field : fields)
2868 {
2869 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2870
2871 if (field.isStruct())
2872 {
2873 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2874 {
2875 const std::string uniformElementName =
2876 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2877 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2878 }
2879 }
2880 else
2881 {
2882 // If getBlockMemberInfo returns false, the uniform is optimized out.
2883 sh::BlockMemberInfo memberInfo;
2884 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2885 {
2886 continue;
2887 }
2888
2889 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize,
2890 blockIndex, memberInfo);
2891
2892 // Since block uniforms have no location, we don't need to store them in the uniform
2893 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002894 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002895 }
2896 }
2897}
2898
Jamie Madill62d31cb2015-09-11 13:25:51 -04002899void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2900{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002901 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002902 size_t blockSize = 0;
2903
2904 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002905 std::stringstream blockNameStr;
2906 blockNameStr << interfaceBlock.name;
2907 if (interfaceBlock.arraySize > 0)
2908 {
2909 blockNameStr << "[0]";
2910 }
2911 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002912 {
2913 return;
2914 }
2915
2916 // Track the first and last uniform index to determine the range of active uniforms in the
2917 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002918 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002919 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002920 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002921
2922 std::vector<unsigned int> blockUniformIndexes;
2923 for (size_t blockUniformIndex = firstBlockUniformIndex;
2924 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2925 {
2926 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2927 }
2928
2929 if (interfaceBlock.arraySize > 0)
2930 {
2931 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2932 {
2933 UniformBlock block(interfaceBlock.name, true, arrayElement);
2934 block.memberUniformIndexes = blockUniformIndexes;
2935
Martin Radev4c4c8e72016-08-04 12:25:34 +03002936 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002937 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002938 case GL_VERTEX_SHADER:
2939 {
2940 block.vertexStaticUse = interfaceBlock.staticUse;
2941 break;
2942 }
2943 case GL_FRAGMENT_SHADER:
2944 {
2945 block.fragmentStaticUse = interfaceBlock.staticUse;
2946 break;
2947 }
2948 case GL_COMPUTE_SHADER:
2949 {
2950 block.computeStaticUse = interfaceBlock.staticUse;
2951 break;
2952 }
2953 default:
2954 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002955 }
2956
Qin Jiajia0350a642016-11-01 17:01:51 +08002957 // Since all block elements in an array share the same active uniforms, they will all be
2958 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2959 // here we will add every block element in the array.
2960 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002961 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002962 }
2963 }
2964 else
2965 {
2966 UniformBlock block(interfaceBlock.name, false, 0);
2967 block.memberUniformIndexes = blockUniformIndexes;
2968
Martin Radev4c4c8e72016-08-04 12:25:34 +03002969 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002970 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002971 case GL_VERTEX_SHADER:
2972 {
2973 block.vertexStaticUse = interfaceBlock.staticUse;
2974 break;
2975 }
2976 case GL_FRAGMENT_SHADER:
2977 {
2978 block.fragmentStaticUse = interfaceBlock.staticUse;
2979 break;
2980 }
2981 case GL_COMPUTE_SHADER:
2982 {
2983 block.computeStaticUse = interfaceBlock.staticUse;
2984 break;
2985 }
2986 default:
2987 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002988 }
2989
Jamie Madill4a3c2342015-10-08 12:58:45 -04002990 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002991 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002992 }
2993}
2994
Jamie Madille7d84322017-01-10 18:21:59 -05002995template <>
2996void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2997 const uint8_t *destPointer,
2998 GLsizei clampedCount,
2999 const GLint *v)
3000{
3001 // Invalidate the validation cache only if we modify the sampler data.
3002 if (mState.isSamplerUniformIndex(locationInfo.index) &&
3003 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
3004 {
3005 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
3006 std::vector<GLuint> *boundTextureUnits =
3007 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
3008
3009 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
3010 mCachedValidateSamplersResult.reset();
3011 }
3012}
3013
3014template <typename T>
3015void Program::updateSamplerUniform(const VariableLocation &locationInfo,
3016 const uint8_t *destPointer,
3017 GLsizei clampedCount,
3018 const T *v)
3019{
3020}
3021
Jamie Madill62d31cb2015-09-11 13:25:51 -04003022template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003023GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003024{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003025 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3026 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003027 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
3028
Corentin Wallez15ac5342016-11-03 17:06:39 -04003029 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3030 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3031 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003032 GLsizei maxElementCount =
3033 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
3034
3035 GLsizei count = countIn;
3036 GLsizei clampedCount = count * vectorSize;
3037 if (clampedCount > maxElementCount)
3038 {
3039 clampedCount = maxElementCount;
3040 count = maxElementCount / vectorSize;
3041 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04003042
Jamie Madill62d31cb2015-09-11 13:25:51 -04003043 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
3044 {
3045 // Do a cast conversion for boolean types. From the spec:
3046 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
3047 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003048 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003049 {
3050 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
3051 }
3052 }
3053 else
3054 {
Jamie Madille7d84322017-01-10 18:21:59 -05003055 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04003056 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003057 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003058
3059 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003060}
3061
3062template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003063GLsizei Program::setMatrixUniformInternal(GLint location,
3064 GLsizei count,
3065 GLboolean transpose,
3066 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003067{
3068 if (!transpose)
3069 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003070 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003071 }
3072
3073 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04003074 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3075 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003076 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04003077
3078 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
3079 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
3080 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
3081 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
3082
3083 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04003084 {
3085 size_t elementOffset = element * rows * cols;
3086
3087 for (size_t row = 0; row < rows; ++row)
3088 {
3089 for (size_t col = 0; col < cols; ++col)
3090 {
3091 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
3092 }
3093 }
3094 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05003095
3096 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04003097}
3098
3099template <typename DestT>
3100void Program::getUniformInternal(GLint location, DestT *dataOut) const
3101{
Jamie Madill48ef11b2016-04-27 15:21:52 -04003102 const VariableLocation &locationInfo = mState.mUniformLocations[location];
3103 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04003104
3105 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
3106
3107 GLenum componentType = VariableComponentType(uniform.type);
3108 if (componentType == GLTypeToGLenum<DestT>::value)
3109 {
3110 memcpy(dataOut, srcPointer, uniform.getElementSize());
3111 return;
3112 }
3113
Corentin Wallez6596c462016-03-17 17:26:58 -04003114 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04003115
3116 switch (componentType)
3117 {
3118 case GL_INT:
3119 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
3120 break;
3121 case GL_UNSIGNED_INT:
3122 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
3123 break;
3124 case GL_BOOL:
3125 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
3126 break;
3127 case GL_FLOAT:
3128 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
3129 break;
3130 default:
3131 UNREACHABLE();
3132 }
3133}
Jamie Madilla4595b82017-01-11 17:36:34 -05003134
3135bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3136{
3137 // Must be called after samplers are validated.
3138 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3139
3140 for (const auto &binding : mState.mSamplerBindings)
3141 {
3142 GLenum textureType = binding.textureType;
3143 for (const auto &unit : binding.boundTextureUnits)
3144 {
3145 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3146 if (programTextureID == textureID)
3147 {
3148 // TODO(jmadill): Check for appropriate overlap.
3149 return true;
3150 }
3151 }
3152 }
3153
3154 return false;
3155}
3156
Jamie Madilla2c74982016-12-12 11:20:42 -05003157} // namespace gl