blob: c1c5605bc45a4f39b9bf3bab8af066f4034f59ba [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"
Olli Etuahob78707c2017-03-09 15:03:11 +000028#include "libANGLE/UniformLinker.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace gl
31{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +000032
Geoff Lang7dd2e102014-11-10 15:19:26 -050033namespace
34{
35
Jamie Madill62d31cb2015-09-11 13:25:51 -040036void WriteShaderVar(BinaryOutputStream *stream, const sh::ShaderVariable &var)
37{
38 stream->writeInt(var.type);
39 stream->writeInt(var.precision);
40 stream->writeString(var.name);
41 stream->writeString(var.mappedName);
42 stream->writeInt(var.arraySize);
43 stream->writeInt(var.staticUse);
44 stream->writeString(var.structName);
45 ASSERT(var.fields.empty());
Geoff Lang7dd2e102014-11-10 15:19:26 -050046}
47
Jamie Madill62d31cb2015-09-11 13:25:51 -040048void LoadShaderVar(BinaryInputStream *stream, sh::ShaderVariable *var)
49{
50 var->type = stream->readInt<GLenum>();
51 var->precision = stream->readInt<GLenum>();
52 var->name = stream->readString();
53 var->mappedName = stream->readString();
54 var->arraySize = stream->readInt<unsigned int>();
55 var->staticUse = stream->readBool();
56 var->structName = stream->readString();
57}
58
Jamie Madill62d31cb2015-09-11 13:25:51 -040059// This simplified cast function doesn't need to worry about advanced concepts like
60// depth range values, or casting to bool.
61template <typename DestT, typename SrcT>
62DestT UniformStateQueryCast(SrcT value);
63
64// From-Float-To-Integer Casts
65template <>
66GLint UniformStateQueryCast(GLfloat value)
67{
68 return clampCast<GLint>(roundf(value));
69}
70
71template <>
72GLuint UniformStateQueryCast(GLfloat value)
73{
74 return clampCast<GLuint>(roundf(value));
75}
76
77// From-Integer-to-Integer Casts
78template <>
79GLint UniformStateQueryCast(GLuint value)
80{
81 return clampCast<GLint>(value);
82}
83
84template <>
85GLuint UniformStateQueryCast(GLint value)
86{
87 return clampCast<GLuint>(value);
88}
89
90// From-Boolean-to-Anything Casts
91template <>
92GLfloat UniformStateQueryCast(GLboolean value)
93{
94 return (value == GL_TRUE ? 1.0f : 0.0f);
95}
96
97template <>
98GLint UniformStateQueryCast(GLboolean value)
99{
100 return (value == GL_TRUE ? 1 : 0);
101}
102
103template <>
104GLuint UniformStateQueryCast(GLboolean value)
105{
106 return (value == GL_TRUE ? 1u : 0u);
107}
108
109// Default to static_cast
110template <typename DestT, typename SrcT>
111DestT UniformStateQueryCast(SrcT value)
112{
113 return static_cast<DestT>(value);
114}
115
116template <typename SrcT, typename DestT>
117void UniformStateQueryCastLoop(DestT *dataOut, const uint8_t *srcPointer, int components)
118{
119 for (int comp = 0; comp < components; ++comp)
120 {
121 // We only work with strides of 4 bytes for uniform components. (GLfloat/GLint)
122 // Don't use SrcT stride directly since GLboolean has a stride of 1 byte.
123 size_t offset = comp * 4;
124 const SrcT *typedSrcPointer = reinterpret_cast<const SrcT *>(&srcPointer[offset]);
125 dataOut[comp] = UniformStateQueryCast<DestT>(*typedSrcPointer);
126 }
127}
128
Jamie Madill192745a2016-12-22 15:58:21 -0500129// true if varying x has a higher priority in packing than y
130bool ComparePackedVarying(const PackedVarying &x, const PackedVarying &y)
131{
132 return gl::CompareShaderVar(*x.varying, *y.varying);
133}
134
Jamie Madill62d31cb2015-09-11 13:25:51 -0400135} // anonymous namespace
136
Jamie Madill4a3c2342015-10-08 12:58:45 -0400137const char *const g_fakepath = "C:\\fakepath";
138
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400139InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000140{
141}
142
143InfoLog::~InfoLog()
144{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000145}
146
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400147size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000148{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400149 const std::string &logString = mStream.str();
150 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000151}
152
Geoff Lange1a27752015-10-05 13:16:04 -0400153void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000154{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400155 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000156
157 if (bufSize > 0)
158 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400159 const std::string str(mStream.str());
160
161 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000162 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400163 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
164 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000165 }
166
167 infoLog[index] = '\0';
168 }
169
170 if (length)
171 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400172 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000173 }
174}
175
176// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300177// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000178// messages, so lets remove all occurrences of this fake file path from the log.
179void InfoLog::appendSanitized(const char *message)
180{
181 std::string msg(message);
182
183 size_t found;
184 do
185 {
186 found = msg.find(g_fakepath);
187 if (found != std::string::npos)
188 {
189 msg.erase(found, strlen(g_fakepath));
190 }
191 }
192 while (found != std::string::npos);
193
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400194 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000195}
196
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000197void InfoLog::reset()
198{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000199}
200
Geoff Langd8605522016-04-13 10:19:12 -0400201VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000202{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500203}
204
Geoff Langd8605522016-04-13 10:19:12 -0400205VariableLocation::VariableLocation(const std::string &name,
206 unsigned int element,
207 unsigned int index)
208 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500209{
210}
211
Geoff Langd8605522016-04-13 10:19:12 -0400212void Program::Bindings::bindLocation(GLuint index, const std::string &name)
213{
214 mBindings[name] = index;
215}
216
217int Program::Bindings::getBinding(const std::string &name) const
218{
219 auto iter = mBindings.find(name);
220 return (iter != mBindings.end()) ? iter->second : -1;
221}
222
223Program::Bindings::const_iterator Program::Bindings::begin() const
224{
225 return mBindings.begin();
226}
227
228Program::Bindings::const_iterator Program::Bindings::end() const
229{
230 return mBindings.end();
231}
232
Jamie Madill48ef11b2016-04-27 15:21:52 -0400233ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500234 : mLabel(),
235 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400236 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300237 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500238 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500239 mSamplerUniformRange(0, 0),
Geoff Langc5629752015-12-07 16:29:04 -0500240 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400241{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300242 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400243}
244
Jamie Madill48ef11b2016-04-27 15:21:52 -0400245ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400246{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500247 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400248}
249
Jamie Madill48ef11b2016-04-27 15:21:52 -0400250const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500251{
252 return mLabel;
253}
254
Jamie Madill48ef11b2016-04-27 15:21:52 -0400255GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400256{
257 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -0500258 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400259
260 for (size_t location = 0; location < mUniformLocations.size(); ++location)
261 {
262 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400263 if (!uniformLocation.used)
264 {
265 continue;
266 }
267
268 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400269
270 if (uniform.name == baseName)
271 {
Geoff Langd8605522016-04-13 10:19:12 -0400272 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400273 {
Geoff Langd8605522016-04-13 10:19:12 -0400274 if (uniformLocation.element == subscript ||
275 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
276 {
277 return static_cast<GLint>(location);
278 }
279 }
280 else
281 {
282 if (subscript == GL_INVALID_INDEX)
283 {
284 return static_cast<GLint>(location);
285 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400286 }
287 }
288 }
289
290 return -1;
291}
292
Jamie Madille7d84322017-01-10 18:21:59 -0500293GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400294{
295 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -0500296 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400297
298 // The app is not allowed to specify array indices other than 0 for arrays of basic types
299 if (subscript != 0 && subscript != GL_INVALID_INDEX)
300 {
301 return GL_INVALID_INDEX;
302 }
303
304 for (size_t index = 0; index < mUniforms.size(); index++)
305 {
306 const LinkedUniform &uniform = mUniforms[index];
307 if (uniform.name == baseName)
308 {
309 if (uniform.isArray() || subscript == GL_INVALID_INDEX)
310 {
311 return static_cast<GLuint>(index);
312 }
313 }
314 }
315
316 return GL_INVALID_INDEX;
317}
318
Jamie Madille7d84322017-01-10 18:21:59 -0500319GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
320{
321 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
322 return mUniformLocations[location].index;
323}
324
325Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
326{
327 GLuint index = getUniformIndexFromLocation(location);
328 if (!isSamplerUniformIndex(index))
329 {
330 return Optional<GLuint>::Invalid();
331 }
332
333 return getSamplerIndexFromUniformIndex(index);
334}
335
336bool ProgramState::isSamplerUniformIndex(GLuint index) const
337{
338 return index >= mSamplerUniformRange.start && index < mSamplerUniformRange.end;
339}
340
341GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
342{
343 ASSERT(isSamplerUniformIndex(uniformIndex));
344 return uniformIndex - mSamplerUniformRange.start;
345}
346
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500347Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400348 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400349 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500350 mLinked(false),
351 mDeleteStatus(false),
352 mRefCount(0),
353 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500354 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500355{
356 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000357
358 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500359 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
362Program::~Program()
363{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500364 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
365 !mState.mAttachedComputeShader);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500366 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367}
368
Jamie Madill6c1f6712017-02-14 19:08:04 -0500369void Program::destroy(const Context *context)
370{
371 if (mState.mAttachedVertexShader != nullptr)
372 {
373 mState.mAttachedVertexShader->release(context);
374 mState.mAttachedVertexShader = nullptr;
375 }
376
377 if (mState.mAttachedFragmentShader != nullptr)
378 {
379 mState.mAttachedFragmentShader->release(context);
380 mState.mAttachedFragmentShader = nullptr;
381 }
382
383 if (mState.mAttachedComputeShader != nullptr)
384 {
385 mState.mAttachedComputeShader->release(context);
386 mState.mAttachedComputeShader = nullptr;
387 }
388
389 mProgram->destroy(rx::SafeGetImpl(context));
390}
391
Geoff Lang70d0f492015-12-10 17:45:46 -0500392void Program::setLabel(const std::string &label)
393{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400394 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500395}
396
397const std::string &Program::getLabel() const
398{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400399 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500400}
401
Jamie Madillef300b12016-10-07 15:12:09 -0400402void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300404 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300406 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407 {
Jamie Madillef300b12016-10-07 15:12:09 -0400408 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300409 mState.mAttachedVertexShader = shader;
410 mState.mAttachedVertexShader->addRef();
411 break;
412 }
413 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414 {
Jamie Madillef300b12016-10-07 15:12:09 -0400415 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300416 mState.mAttachedFragmentShader = shader;
417 mState.mAttachedFragmentShader->addRef();
418 break;
419 }
420 case GL_COMPUTE_SHADER:
421 {
Jamie Madillef300b12016-10-07 15:12:09 -0400422 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300423 mState.mAttachedComputeShader = shader;
424 mState.mAttachedComputeShader->addRef();
425 break;
426 }
427 default:
428 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430}
431
Jamie Madill6c1f6712017-02-14 19:08:04 -0500432bool Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300434 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300436 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300438 if (mState.mAttachedVertexShader != shader)
439 {
440 return false;
441 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442
Jamie Madill6c1f6712017-02-14 19:08:04 -0500443 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300444 mState.mAttachedVertexShader = nullptr;
445 break;
446 }
447 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300449 if (mState.mAttachedFragmentShader != shader)
450 {
451 return false;
452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453
Jamie Madill6c1f6712017-02-14 19:08:04 -0500454 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300455 mState.mAttachedFragmentShader = nullptr;
456 break;
457 }
458 case GL_COMPUTE_SHADER:
459 {
460 if (mState.mAttachedComputeShader != shader)
461 {
462 return false;
463 }
464
Jamie Madill6c1f6712017-02-14 19:08:04 -0500465 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300466 mState.mAttachedComputeShader = nullptr;
467 break;
468 }
469 default:
470 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473 return true;
474}
475
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000476int Program::getAttachedShadersCount() const
477{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300478 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
479 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000480}
481
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482void Program::bindAttributeLocation(GLuint index, const char *name)
483{
Geoff Langd8605522016-04-13 10:19:12 -0400484 mAttributeBindings.bindLocation(index, name);
485}
486
487void Program::bindUniformLocation(GLuint index, const char *name)
488{
489 // Bind the base uniform name only since array indices other than 0 cannot be bound
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000490 mUniformLocationBindings.bindLocation(index, ParseUniformName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491}
492
Sami Väisänen46eaa942016-06-29 10:26:37 +0300493void Program::bindFragmentInputLocation(GLint index, const char *name)
494{
495 mFragmentInputBindings.bindLocation(index, name);
496}
497
498BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
499{
500 BindingInfo ret;
501 ret.type = GL_NONE;
502 ret.valid = false;
503
504 const Shader *fragmentShader = mState.getAttachedFragmentShader();
505 ASSERT(fragmentShader);
506
507 // Find the actual fragment shader varying we're interested in
508 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
509
510 for (const auto &binding : mFragmentInputBindings)
511 {
512 if (binding.second != static_cast<GLuint>(index))
513 continue;
514
515 ret.valid = true;
516
517 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400518 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300519
520 for (const auto &in : inputs)
521 {
522 if (in.name == originalName)
523 {
524 if (in.isArray())
525 {
526 // The client wants to bind either "name" or "name[0]".
527 // GL ES 3.1 spec refers to active array names with language such as:
528 // "if the string identifies the base name of an active array, where the
529 // string would exactly match the name of the variable if the suffix "[0]"
530 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400531 if (arrayIndex == GL_INVALID_INDEX)
532 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300533
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400534 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300535 }
536 else
537 {
538 ret.name = in.mappedName;
539 }
540 ret.type = in.type;
541 return ret;
542 }
543 }
544 }
545
546 return ret;
547}
548
549void Program::pathFragmentInputGen(GLint index,
550 GLenum genMode,
551 GLint components,
552 const GLfloat *coeffs)
553{
554 // If the location is -1 then the command is silently ignored
555 if (index == -1)
556 return;
557
558 const auto &binding = getFragmentInputBindingInfo(index);
559
560 // If the input doesn't exist then then the command is silently ignored
561 // This could happen through optimization for example, the shader translator
562 // decides that a variable is not actually being used and optimizes it away.
563 if (binding.name.empty())
564 return;
565
566 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
567}
568
Martin Radev4c4c8e72016-08-04 12:25:34 +0300569// The attached shaders are checked for linking errors by matching up their variables.
570// Uniform, input and output variables get collected.
571// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500572Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000573{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500574 const auto &data = context->getContextState();
575
Jamie Madill6c1f6712017-02-14 19:08:04 -0500576 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000577
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000578 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000579 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000580
Martin Radev4c4c8e72016-08-04 12:25:34 +0300581 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500582
Jamie Madill192745a2016-12-22 15:58:21 -0500583 auto vertexShader = mState.mAttachedVertexShader;
584 auto fragmentShader = mState.mAttachedFragmentShader;
585 auto computeShader = mState.mAttachedComputeShader;
586
587 bool isComputeShaderAttached = (computeShader != nullptr);
588 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300589 // Check whether we both have a compute and non-compute shaders attached.
590 // If there are of both types attached, then linking should fail.
591 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
592 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500593 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300594 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
595 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400596 }
597
Jamie Madill192745a2016-12-22 15:58:21 -0500598 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500599 {
Jamie Madill192745a2016-12-22 15:58:21 -0500600 if (!computeShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300601 {
602 mInfoLog << "Attached compute shader is not compiled.";
603 return NoError();
604 }
Jamie Madill192745a2016-12-22 15:58:21 -0500605 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300606
Jamie Madill192745a2016-12-22 15:58:21 -0500607 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300608
609 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
610 // If the work group size is not specified, a link time error should occur.
611 if (!mState.mComputeShaderLocalSize.isDeclared())
612 {
613 mInfoLog << "Work group size is not specified.";
614 return NoError();
615 }
616
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000617 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300618 {
619 return NoError();
620 }
621
622 if (!linkUniformBlocks(mInfoLog, caps))
623 {
624 return NoError();
625 }
626
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500627 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
628 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), noPacking, mInfoLog),
629 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500630 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300631 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500632 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300633 }
634 }
635 else
636 {
Jamie Madill192745a2016-12-22 15:58:21 -0500637 if (!fragmentShader || !fragmentShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300638 {
639 return NoError();
640 }
Jamie Madill192745a2016-12-22 15:58:21 -0500641 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300642
Jamie Madill192745a2016-12-22 15:58:21 -0500643 if (!vertexShader || !vertexShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300644 {
645 return NoError();
646 }
Jamie Madill192745a2016-12-22 15:58:21 -0500647 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300648
Jamie Madill192745a2016-12-22 15:58:21 -0500649 if (fragmentShader->getShaderVersion() != vertexShader->getShaderVersion())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300650 {
651 mInfoLog << "Fragment shader version does not match vertex shader version.";
652 return NoError();
653 }
654
Jamie Madilleb979bf2016-11-15 12:28:46 -0500655 if (!linkAttributes(data, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300656 {
657 return NoError();
658 }
659
Jamie Madill192745a2016-12-22 15:58:21 -0500660 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300661 {
662 return NoError();
663 }
664
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000665 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300666 {
667 return NoError();
668 }
669
670 if (!linkUniformBlocks(mInfoLog, caps))
671 {
672 return NoError();
673 }
674
675 const auto &mergedVaryings = getMergedVaryings();
676
677 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
678 {
679 return NoError();
680 }
681
682 linkOutputVariables();
683
Jamie Madill192745a2016-12-22 15:58:21 -0500684 // Validate we can pack the varyings.
685 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
686
687 // Map the varyings to the register file
688 // In WebGL, we use a slightly different handling for packing variables.
689 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
690 : PackMode::ANGLE_RELAXED;
691 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
692 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
693 mState.getTransformFeedbackVaryingNames()))
694 {
695 return NoError();
696 }
697
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500698 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), varyingPacking, mInfoLog),
699 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500700 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300701 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500702 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300703 }
704
705 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500706 }
707
Jamie Madill4a3c2342015-10-08 12:58:45 -0400708 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400709
Martin Radev4c4c8e72016-08-04 12:25:34 +0300710 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000711}
712
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000713// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500714void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000715{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400716 mState.mAttributes.clear();
717 mState.mActiveAttribLocationsMask.reset();
718 mState.mTransformFeedbackVaryingVars.clear();
719 mState.mUniforms.clear();
720 mState.mUniformLocations.clear();
721 mState.mUniformBlocks.clear();
722 mState.mOutputVariables.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300723 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500724 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500725
Geoff Lang7dd2e102014-11-10 15:19:26 -0500726 mValidated = false;
727
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000728 mLinked = false;
729}
730
Geoff Lange1a27752015-10-05 13:16:04 -0400731bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000732{
733 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734}
735
Jamie Madilla2c74982016-12-12 11:20:42 -0500736Error Program::loadBinary(const Context *context,
737 GLenum binaryFormat,
738 const void *binary,
739 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000740{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500741 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000742
Geoff Lang7dd2e102014-11-10 15:19:26 -0500743#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800744 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500745#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400746 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
747 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000748 {
Jamie Madillf6113162015-05-07 11:49:21 -0400749 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800750 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500751 }
752
Geoff Langc46cc2f2015-10-01 17:16:20 -0400753 BinaryInputStream stream(binary, length);
754
Jamie Madilla2c74982016-12-12 11:20:42 -0500755 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
756 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
757 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) !=
758 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500759 {
Jamie Madillf6113162015-05-07 11:49:21 -0400760 mInfoLog << "Invalid program binary version.";
He Yunchaoacd18982017-01-04 10:46:42 +0800761 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500762 }
763
Jamie Madilla2c74982016-12-12 11:20:42 -0500764 int majorVersion = stream.readInt<int>();
765 int minorVersion = stream.readInt<int>();
766 if (majorVersion != context->getClientMajorVersion() ||
767 minorVersion != context->getClientMinorVersion())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500768 {
Jamie Madilla2c74982016-12-12 11:20:42 -0500769 mInfoLog << "Cannot load program binaries across different ES context versions.";
He Yunchaoacd18982017-01-04 10:46:42 +0800770 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500771 }
772
Martin Radev4c4c8e72016-08-04 12:25:34 +0300773 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
774 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
775 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
776
Jamie Madill63805b42015-08-25 13:17:39 -0400777 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
778 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400779 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500780
Jamie Madill3da79b72015-04-27 11:09:17 -0400781 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400782 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400783 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
784 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400785 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400786 LoadShaderVar(&stream, &attrib);
787 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400788 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400789 }
790
Jamie Madill62d31cb2015-09-11 13:25:51 -0400791 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400792 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400793 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
794 {
795 LinkedUniform uniform;
796 LoadShaderVar(&stream, &uniform);
797
798 uniform.blockIndex = stream.readInt<int>();
799 uniform.blockInfo.offset = stream.readInt<int>();
800 uniform.blockInfo.arrayStride = stream.readInt<int>();
801 uniform.blockInfo.matrixStride = stream.readInt<int>();
802 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
803
Jamie Madill48ef11b2016-04-27 15:21:52 -0400804 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400805 }
806
807 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400808 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400809 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
810 uniformIndexIndex++)
811 {
812 VariableLocation variable;
813 stream.readString(&variable.name);
814 stream.readInt(&variable.element);
815 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400816 stream.readBool(&variable.used);
817 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400818
Jamie Madill48ef11b2016-04-27 15:21:52 -0400819 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400820 }
821
822 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400823 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400824 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
825 ++uniformBlockIndex)
826 {
827 UniformBlock uniformBlock;
828 stream.readString(&uniformBlock.name);
829 stream.readBool(&uniformBlock.isArray);
830 stream.readInt(&uniformBlock.arrayElement);
831 stream.readInt(&uniformBlock.dataSize);
832 stream.readBool(&uniformBlock.vertexStaticUse);
833 stream.readBool(&uniformBlock.fragmentStaticUse);
834
835 unsigned int numMembers = stream.readInt<unsigned int>();
836 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
837 {
838 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
839 }
840
Jamie Madill48ef11b2016-04-27 15:21:52 -0400841 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400842 }
843
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500844 for (GLuint bindingIndex = 0; bindingIndex < mState.mUniformBlockBindings.size();
845 ++bindingIndex)
846 {
847 stream.readInt(&mState.mUniformBlockBindings[bindingIndex]);
848 mState.mActiveUniformBlockBindings.set(bindingIndex,
849 mState.mUniformBlockBindings[bindingIndex] != 0);
850 }
851
Brandon Jones1048ea72015-10-06 15:34:52 -0700852 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400853 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700854 for (unsigned int transformFeedbackVaryingIndex = 0;
855 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
856 ++transformFeedbackVaryingIndex)
857 {
858 sh::Varying varying;
859 stream.readInt(&varying.arraySize);
860 stream.readInt(&varying.type);
861 stream.readString(&varying.name);
862
Jamie Madill48ef11b2016-04-27 15:21:52 -0400863 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700864 }
865
Jamie Madill48ef11b2016-04-27 15:21:52 -0400866 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400867
Jamie Madill80a6fc02015-08-21 16:53:16 -0400868 unsigned int outputVarCount = stream.readInt<unsigned int>();
869 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
870 {
871 int locationIndex = stream.readInt<int>();
872 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400873 stream.readInt(&locationData.element);
874 stream.readInt(&locationData.index);
875 stream.readString(&locationData.name);
Jamie Madill48ef11b2016-04-27 15:21:52 -0400876 mState.mOutputVariables[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400877 }
878
Jamie Madille7d84322017-01-10 18:21:59 -0500879 stream.readInt(&mState.mSamplerUniformRange.start);
880 stream.readInt(&mState.mSamplerUniformRange.end);
881
882 unsigned int samplerCount = stream.readInt<unsigned int>();
883 for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex)
884 {
885 GLenum textureType = stream.readInt<GLenum>();
886 size_t bindingCount = stream.readInt<size_t>();
887 mState.mSamplerBindings.emplace_back(SamplerBinding(textureType, bindingCount));
888 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400889
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500890 ANGLE_TRY_RESULT(mProgram->load(context->getImplementation(), mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000891
Jamie Madillb0a838b2016-11-13 20:02:12 -0500892 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500893#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500894}
895
Jamie Madilla2c74982016-12-12 11:20:42 -0500896Error Program::saveBinary(const Context *context,
897 GLenum *binaryFormat,
898 void *binary,
899 GLsizei bufSize,
900 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500901{
902 if (binaryFormat)
903 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400904 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500905 }
906
907 BinaryOutputStream stream;
908
Geoff Lang7dd2e102014-11-10 15:19:26 -0500909 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
910
Jamie Madilla2c74982016-12-12 11:20:42 -0500911 // nullptr context is supported when computing binary length.
912 if (context)
913 {
914 stream.writeInt(context->getClientVersion().major);
915 stream.writeInt(context->getClientVersion().minor);
916 }
917 else
918 {
919 stream.writeInt(2);
920 stream.writeInt(0);
921 }
922
Martin Radev4c4c8e72016-08-04 12:25:34 +0300923 stream.writeInt(mState.mComputeShaderLocalSize[0]);
924 stream.writeInt(mState.mComputeShaderLocalSize[1]);
925 stream.writeInt(mState.mComputeShaderLocalSize[2]);
926
Jamie Madill48ef11b2016-04-27 15:21:52 -0400927 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500928
Jamie Madill48ef11b2016-04-27 15:21:52 -0400929 stream.writeInt(mState.mAttributes.size());
930 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400931 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400932 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400933 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400934 }
935
Jamie Madill48ef11b2016-04-27 15:21:52 -0400936 stream.writeInt(mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -0500937 for (const LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400938 {
939 WriteShaderVar(&stream, uniform);
940
941 // FIXME: referenced
942
943 stream.writeInt(uniform.blockIndex);
944 stream.writeInt(uniform.blockInfo.offset);
945 stream.writeInt(uniform.blockInfo.arrayStride);
946 stream.writeInt(uniform.blockInfo.matrixStride);
947 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
948 }
949
Jamie Madill48ef11b2016-04-27 15:21:52 -0400950 stream.writeInt(mState.mUniformLocations.size());
951 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400952 {
953 stream.writeString(variable.name);
954 stream.writeInt(variable.element);
955 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400956 stream.writeInt(variable.used);
957 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400958 }
959
Jamie Madill48ef11b2016-04-27 15:21:52 -0400960 stream.writeInt(mState.mUniformBlocks.size());
961 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400962 {
963 stream.writeString(uniformBlock.name);
964 stream.writeInt(uniformBlock.isArray);
965 stream.writeInt(uniformBlock.arrayElement);
966 stream.writeInt(uniformBlock.dataSize);
967
968 stream.writeInt(uniformBlock.vertexStaticUse);
969 stream.writeInt(uniformBlock.fragmentStaticUse);
970
971 stream.writeInt(uniformBlock.memberUniformIndexes.size());
972 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
973 {
974 stream.writeInt(memberUniformIndex);
975 }
Jamie Madill3da79b72015-04-27 11:09:17 -0400976 }
977
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500978 for (GLuint binding : mState.mUniformBlockBindings)
979 {
980 stream.writeInt(binding);
981 }
982
Jamie Madill48ef11b2016-04-27 15:21:52 -0400983 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
984 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -0700985 {
986 stream.writeInt(varying.arraySize);
987 stream.writeInt(varying.type);
988 stream.writeString(varying.name);
989 }
990
Jamie Madill48ef11b2016-04-27 15:21:52 -0400991 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400992
Jamie Madill48ef11b2016-04-27 15:21:52 -0400993 stream.writeInt(mState.mOutputVariables.size());
994 for (const auto &outputPair : mState.mOutputVariables)
Jamie Madill80a6fc02015-08-21 16:53:16 -0400995 {
996 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -0400997 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -0400998 stream.writeInt(outputPair.second.index);
999 stream.writeString(outputPair.second.name);
1000 }
1001
Jamie Madille7d84322017-01-10 18:21:59 -05001002 stream.writeInt(mState.mSamplerUniformRange.start);
1003 stream.writeInt(mState.mSamplerUniformRange.end);
1004
1005 stream.writeInt(mState.mSamplerBindings.size());
1006 for (const auto &samplerBinding : mState.mSamplerBindings)
1007 {
1008 stream.writeInt(samplerBinding.textureType);
1009 stream.writeInt(samplerBinding.boundTextureUnits.size());
1010 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001011
Jamie Madilla2c74982016-12-12 11:20:42 -05001012 ANGLE_TRY(mProgram->save(&stream));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001013
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001014 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -04001015 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001016
1017 if (streamLength > bufSize)
1018 {
1019 if (length)
1020 {
1021 *length = 0;
1022 }
1023
1024 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1025 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1026 // sizes and then copy it.
1027 return Error(GL_INVALID_OPERATION);
1028 }
1029
1030 if (binary)
1031 {
1032 char *ptr = reinterpret_cast<char*>(binary);
1033
Jamie Madill48ef11b2016-04-27 15:21:52 -04001034 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001035 ptr += streamLength;
1036
1037 ASSERT(ptr - streamLength == binary);
1038 }
1039
1040 if (length)
1041 {
1042 *length = streamLength;
1043 }
1044
He Yunchaoacd18982017-01-04 10:46:42 +08001045 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001046}
1047
1048GLint Program::getBinaryLength() const
1049{
1050 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -05001051 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001052 if (error.isError())
1053 {
1054 return 0;
1055 }
1056
1057 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001058}
1059
Geoff Langc5629752015-12-07 16:29:04 -05001060void Program::setBinaryRetrievableHint(bool retrievable)
1061{
1062 // TODO(jmadill) : replace with dirty bits
1063 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001064 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001065}
1066
1067bool Program::getBinaryRetrievableHint() const
1068{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001069 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001070}
1071
Jamie Madill6c1f6712017-02-14 19:08:04 -05001072void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001073{
1074 mRefCount--;
1075
1076 if (mRefCount == 0 && mDeleteStatus)
1077 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001078 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001079 }
1080}
1081
1082void Program::addRef()
1083{
1084 mRefCount++;
1085}
1086
1087unsigned int Program::getRefCount() const
1088{
1089 return mRefCount;
1090}
1091
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001092int Program::getInfoLogLength() const
1093{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001094 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001095}
1096
Geoff Lange1a27752015-10-05 13:16:04 -04001097void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001098{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001099 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001100}
1101
Geoff Lange1a27752015-10-05 13:16:04 -04001102void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001103{
1104 int total = 0;
1105
Martin Radev4c4c8e72016-08-04 12:25:34 +03001106 if (mState.mAttachedComputeShader)
1107 {
1108 if (total < maxCount)
1109 {
1110 shaders[total] = mState.mAttachedComputeShader->getHandle();
1111 total++;
1112 }
1113 }
1114
Jamie Madill48ef11b2016-04-27 15:21:52 -04001115 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001116 {
1117 if (total < maxCount)
1118 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001119 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001120 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001121 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001122 }
1123
Jamie Madill48ef11b2016-04-27 15:21:52 -04001124 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001125 {
1126 if (total < maxCount)
1127 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001128 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001129 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001130 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001131 }
1132
1133 if (count)
1134 {
1135 *count = total;
1136 }
1137}
1138
Geoff Lange1a27752015-10-05 13:16:04 -04001139GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001140{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001141 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001143 if (attribute.name == name && attribute.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001145 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001146 }
1147 }
1148
Austin Kinrossb8af7232015-03-16 22:33:25 -07001149 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001150}
1151
Jamie Madill63805b42015-08-25 13:17:39 -04001152bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001153{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001154 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1155 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001156}
1157
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001158void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name)
1159{
Jamie Madillc349ec02015-08-21 16:53:12 -04001160 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001161 {
1162 if (bufsize > 0)
1163 {
1164 name[0] = '\0';
1165 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001166
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001167 if (length)
1168 {
1169 *length = 0;
1170 }
1171
1172 *type = GL_NONE;
1173 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001174 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001175 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001176
1177 size_t attributeIndex = 0;
1178
Jamie Madill48ef11b2016-04-27 15:21:52 -04001179 for (const sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001180 {
1181 // Skip over inactive attributes
1182 if (attribute.staticUse)
1183 {
1184 if (static_cast<size_t>(index) == attributeIndex)
1185 {
1186 break;
1187 }
1188 attributeIndex++;
1189 }
1190 }
1191
Jamie Madill48ef11b2016-04-27 15:21:52 -04001192 ASSERT(index == attributeIndex && attributeIndex < mState.mAttributes.size());
1193 const sh::Attribute &attrib = mState.mAttributes[attributeIndex];
Jamie Madillc349ec02015-08-21 16:53:12 -04001194
1195 if (bufsize > 0)
1196 {
1197 const char *string = attrib.name.c_str();
1198
1199 strncpy(name, string, bufsize);
1200 name[bufsize - 1] = '\0';
1201
1202 if (length)
1203 {
1204 *length = static_cast<GLsizei>(strlen(name));
1205 }
1206 }
1207
1208 // Always a single 'type' instance
1209 *size = 1;
1210 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001211}
1212
Geoff Lange1a27752015-10-05 13:16:04 -04001213GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001214{
Jamie Madillc349ec02015-08-21 16:53:12 -04001215 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001216 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001217 return 0;
1218 }
1219
1220 GLint count = 0;
1221
Jamie Madill48ef11b2016-04-27 15:21:52 -04001222 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001223 {
1224 count += (attrib.staticUse ? 1 : 0);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001225 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001226
1227 return count;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001228}
1229
Geoff Lange1a27752015-10-05 13:16:04 -04001230GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001231{
Jamie Madillc349ec02015-08-21 16:53:12 -04001232 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001233 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001234 return 0;
1235 }
1236
1237 size_t maxLength = 0;
1238
Jamie Madill48ef11b2016-04-27 15:21:52 -04001239 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001240 {
1241 if (attrib.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001242 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001243 maxLength = std::max(attrib.name.length() + 1, maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001244 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001245 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001246
Jamie Madillc349ec02015-08-21 16:53:12 -04001247 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001248}
1249
Geoff Lang7dd2e102014-11-10 15:19:26 -05001250GLint Program::getFragDataLocation(const std::string &name) const
1251{
1252 std::string baseName(name);
1253 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001254 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001255 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001256 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001257 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1258 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001259 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001260 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001261 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001262 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001263}
1264
Geoff Lange1a27752015-10-05 13:16:04 -04001265void Program::getActiveUniform(GLuint index,
1266 GLsizei bufsize,
1267 GLsizei *length,
1268 GLint *size,
1269 GLenum *type,
1270 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001271{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001272 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001273 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001274 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001275 ASSERT(index < mState.mUniforms.size());
1276 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001277
1278 if (bufsize > 0)
1279 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001280 std::string string = uniform.name;
1281 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001282 {
1283 string += "[0]";
1284 }
1285
1286 strncpy(name, string.c_str(), bufsize);
1287 name[bufsize - 1] = '\0';
1288
1289 if (length)
1290 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001291 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292 }
1293 }
1294
Jamie Madill62d31cb2015-09-11 13:25:51 -04001295 *size = uniform.elementCount();
1296 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001297 }
1298 else
1299 {
1300 if (bufsize > 0)
1301 {
1302 name[0] = '\0';
1303 }
1304
1305 if (length)
1306 {
1307 *length = 0;
1308 }
1309
1310 *size = 0;
1311 *type = GL_NONE;
1312 }
1313}
1314
Geoff Lange1a27752015-10-05 13:16:04 -04001315GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001316{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001317 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001318 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001319 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001320 }
1321 else
1322 {
1323 return 0;
1324 }
1325}
1326
Geoff Lange1a27752015-10-05 13:16:04 -04001327GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001328{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001329 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001330
1331 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001332 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001333 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001334 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001335 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001336 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001337 size_t length = uniform.name.length() + 1u;
1338 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001339 {
1340 length += 3; // Counting in "[0]".
1341 }
1342 maxLength = std::max(length, maxLength);
1343 }
1344 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001345 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001346
Jamie Madill62d31cb2015-09-11 13:25:51 -04001347 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001348}
1349
1350GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1351{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001352 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001353 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001354 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001355 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001356 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1357 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1358 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1359 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1360 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1361 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1362 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1363 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1364 default:
1365 UNREACHABLE();
1366 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001367 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001368 return 0;
1369}
1370
1371bool Program::isValidUniformLocation(GLint location) const
1372{
Jamie Madille2e406c2016-06-02 13:04:10 -04001373 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001374 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1375 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001376}
1377
Jamie Madill62d31cb2015-09-11 13:25:51 -04001378const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001380 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001381 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382}
1383
Jamie Madillac4e9c32017-01-13 14:07:12 -05001384const VariableLocation &Program::getUniformLocation(GLint location) const
1385{
1386 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1387 return mState.mUniformLocations[location];
1388}
1389
1390const std::vector<VariableLocation> &Program::getUniformLocations() const
1391{
1392 return mState.mUniformLocations;
1393}
1394
1395const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1396{
1397 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1398 return mState.mUniforms[index];
1399}
1400
Jamie Madill62d31cb2015-09-11 13:25:51 -04001401GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001403 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001404}
1405
Jamie Madill62d31cb2015-09-11 13:25:51 -04001406GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001407{
Jamie Madille7d84322017-01-10 18:21:59 -05001408 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001409}
1410
1411void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1412{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001413 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1414 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001415}
1416
1417void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1418{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001419 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1420 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001421}
1422
1423void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1424{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001425 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1426 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001427}
1428
1429void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1430{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001431 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1432 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001433}
1434
1435void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1436{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001437 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1438 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001439}
1440
1441void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1442{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001443 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1444 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001445}
1446
1447void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1448{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001449 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1450 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001451}
1452
1453void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1454{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001455 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1456 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001457}
1458
1459void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1460{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001461 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1462 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001463}
1464
1465void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1466{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001467 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1468 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001469}
1470
1471void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1472{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001473 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1474 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001475}
1476
1477void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1478{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001479 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1480 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001481}
1482
1483void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1484{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001485 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1486 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001487}
1488
1489void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1490{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001491 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1492 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001493}
1494
1495void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1496{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001497 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1498 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001499}
1500
1501void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1502{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001503 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1504 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001505}
1506
1507void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1508{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001509 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1510 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001511}
1512
1513void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1514{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001515 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1516 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001517}
1518
1519void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1520{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001521 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1522 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001523}
1524
1525void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1526{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001527 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1528 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001529}
1530
1531void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1532{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001533 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1534 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001535}
1536
Geoff Lange1a27752015-10-05 13:16:04 -04001537void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001538{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001539 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001540}
1541
Geoff Lange1a27752015-10-05 13:16:04 -04001542void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001543{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001544 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001545}
1546
Geoff Lange1a27752015-10-05 13:16:04 -04001547void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001548{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001549 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001550}
1551
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001552void Program::flagForDeletion()
1553{
1554 mDeleteStatus = true;
1555}
1556
1557bool Program::isFlaggedForDeletion() const
1558{
1559 return mDeleteStatus;
1560}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001561
Brandon Jones43a53e22014-08-28 16:23:22 -07001562void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001563{
1564 mInfoLog.reset();
1565
Geoff Lang7dd2e102014-11-10 15:19:26 -05001566 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001567 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001568 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001569 }
1570 else
1571 {
Jamie Madillf6113162015-05-07 11:49:21 -04001572 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001573 }
1574}
1575
Geoff Lang7dd2e102014-11-10 15:19:26 -05001576bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1577{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001578 // Skip cache if we're using an infolog, so we get the full error.
1579 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1580 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1581 {
1582 return mCachedValidateSamplersResult.value();
1583 }
1584
1585 if (mTextureUnitTypesCache.empty())
1586 {
1587 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1588 }
1589 else
1590 {
1591 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1592 }
1593
1594 // if any two active samplers in a program are of different types, but refer to the same
1595 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1596 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001597 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001598 {
Jamie Madille7d84322017-01-10 18:21:59 -05001599 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001600
Jamie Madille7d84322017-01-10 18:21:59 -05001601 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001602 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001603 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1604 {
1605 if (infoLog)
1606 {
1607 (*infoLog) << "Sampler uniform (" << textureUnit
1608 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1609 << caps.maxCombinedTextureImageUnits << ")";
1610 }
1611
1612 mCachedValidateSamplersResult = false;
1613 return false;
1614 }
1615
1616 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1617 {
1618 if (textureType != mTextureUnitTypesCache[textureUnit])
1619 {
1620 if (infoLog)
1621 {
1622 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1623 "image unit ("
1624 << textureUnit << ").";
1625 }
1626
1627 mCachedValidateSamplersResult = false;
1628 return false;
1629 }
1630 }
1631 else
1632 {
1633 mTextureUnitTypesCache[textureUnit] = textureType;
1634 }
1635 }
1636 }
1637
1638 mCachedValidateSamplersResult = true;
1639 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001640}
1641
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001642bool Program::isValidated() const
1643{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001644 return mValidated;
1645}
1646
Geoff Lange1a27752015-10-05 13:16:04 -04001647GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001648{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001649 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650}
1651
1652void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1653{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001654 ASSERT(
1655 uniformBlockIndex <
1656 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001657
Jamie Madill48ef11b2016-04-27 15:21:52 -04001658 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001659
1660 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001661 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001662 std::string string = uniformBlock.name;
1663
Jamie Madill62d31cb2015-09-11 13:25:51 -04001664 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001665 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001666 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667 }
1668
1669 strncpy(uniformBlockName, string.c_str(), bufSize);
1670 uniformBlockName[bufSize - 1] = '\0';
1671
1672 if (length)
1673 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001674 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001675 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001676 }
1677}
1678
Geoff Lange1a27752015-10-05 13:16:04 -04001679GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001680{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001681 int maxLength = 0;
1682
1683 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001684 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001685 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001686 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1687 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001688 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001689 if (!uniformBlock.name.empty())
1690 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001691 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001692
1693 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001694 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001695
1696 maxLength = std::max(length + arrayLength, maxLength);
1697 }
1698 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001699 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001700
1701 return maxLength;
1702}
1703
Geoff Lange1a27752015-10-05 13:16:04 -04001704GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001705{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001706 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -05001707 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001708
Jamie Madill48ef11b2016-04-27 15:21:52 -04001709 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001710 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1711 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001712 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001713 if (uniformBlock.name == baseName)
1714 {
1715 const bool arrayElementZero =
1716 (subscript == GL_INVALID_INDEX &&
1717 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1718 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1719 {
1720 return blockIndex;
1721 }
1722 }
1723 }
1724
1725 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001726}
1727
Jamie Madill62d31cb2015-09-11 13:25:51 -04001728const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001729{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001730 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1731 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001732}
1733
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001734void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1735{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001736 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001737 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001738 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001739}
1740
1741GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1742{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001743 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001744}
1745
1746void Program::resetUniformBlockBindings()
1747{
1748 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1749 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001750 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001751 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001752 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001753}
1754
Geoff Lang48dcae72014-02-05 16:28:24 -05001755void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1756{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001757 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001758 for (GLsizei i = 0; i < count; i++)
1759 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001760 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001761 }
1762
Jamie Madill48ef11b2016-04-27 15:21:52 -04001763 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001764}
1765
1766void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1767{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001768 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001769 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001770 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1771 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001772 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1773 if (length)
1774 {
1775 *length = lastNameIdx;
1776 }
1777 if (size)
1778 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001779 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001780 }
1781 if (type)
1782 {
1783 *type = varying.type;
1784 }
1785 if (name)
1786 {
1787 memcpy(name, varying.name.c_str(), lastNameIdx);
1788 name[lastNameIdx] = '\0';
1789 }
1790 }
1791}
1792
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001793GLsizei Program::getTransformFeedbackVaryingCount() const
1794{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001795 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001796 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001797 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001798 }
1799 else
1800 {
1801 return 0;
1802 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001803}
1804
1805GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1806{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001807 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001808 {
1809 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001810 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001811 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001812 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1813 }
1814
1815 return maxSize;
1816 }
1817 else
1818 {
1819 return 0;
1820 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001821}
1822
1823GLenum Program::getTransformFeedbackBufferMode() const
1824{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001825 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826}
1827
Jamie Madill192745a2016-12-22 15:58:21 -05001828bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001829{
Jamie Madill192745a2016-12-22 15:58:21 -05001830 const Shader *vertexShader = mState.mAttachedVertexShader;
1831 const Shader *fragmentShader = mState.mAttachedFragmentShader;
1832
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001833 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1834
Jamie Madill4cff2472015-08-21 16:53:18 -04001835 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1836 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001837
Sami Väisänen46eaa942016-06-29 10:26:37 +03001838 std::map<GLuint, std::string> staticFragmentInputLocations;
1839
Jamie Madill4cff2472015-08-21 16:53:18 -04001840 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001842 bool matched = false;
1843
1844 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001845 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001846 {
1847 continue;
1848 }
1849
Jamie Madill4cff2472015-08-21 16:53:18 -04001850 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001851 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001852 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001853 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001854 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001855 if (!linkValidateVaryings(infoLog, output.name, input, output,
1856 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001857 {
1858 return false;
1859 }
1860
Geoff Lang7dd2e102014-11-10 15:19:26 -05001861 matched = true;
1862 break;
1863 }
1864 }
1865
1866 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001867 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001869 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001870 return false;
1871 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001872
1873 // Check for aliased path rendering input bindings (if any).
1874 // If more than one binding refer statically to the same
1875 // location the link must fail.
1876
1877 if (!output.staticUse)
1878 continue;
1879
1880 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1881 if (inputBinding == -1)
1882 continue;
1883
1884 const auto it = staticFragmentInputLocations.find(inputBinding);
1885 if (it == std::end(staticFragmentInputLocations))
1886 {
1887 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1888 }
1889 else
1890 {
1891 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1892 << it->second;
1893 return false;
1894 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001895 }
1896
Yuly Novikov817232e2017-02-22 18:36:10 -05001897 if (!linkValidateBuiltInVaryings(infoLog))
1898 {
1899 return false;
1900 }
1901
Jamie Madillada9ecc2015-08-17 12:53:37 -04001902 // TODO(jmadill): verify no unmatched vertex varyings?
1903
Geoff Lang7dd2e102014-11-10 15:19:26 -05001904 return true;
1905}
1906
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001907bool Program::linkUniforms(InfoLog &infoLog,
1908 const Caps &caps,
1909 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001910{
Olli Etuahob78707c2017-03-09 15:03:11 +00001911 UniformLinker linker(mState);
1912 if (!linker.link(infoLog, caps, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001913 {
1914 return false;
1915 }
1916
Olli Etuahob78707c2017-03-09 15:03:11 +00001917 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001918
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001919 updateSamplerBindings();
1920
1921 return true;
1922}
1923
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001924void Program::updateSamplerBindings()
1925{
1926 mState.mSamplerUniformRange.end = static_cast<unsigned int>(mState.mUniforms.size());
1927 mState.mSamplerUniformRange.start = mState.mSamplerUniformRange.end;
1928 auto samplerIter = mState.mUniforms.rbegin();
1929 while (samplerIter != mState.mUniforms.rend() && samplerIter->isSampler())
1930 {
1931 --mState.mSamplerUniformRange.start;
1932 ++samplerIter;
1933 }
1934 // If uniform is a sampler type, insert it into the mSamplerBindings array.
1935 for (unsigned int samplerIndex = mState.mSamplerUniformRange.start;
1936 samplerIndex < mState.mUniforms.size(); ++samplerIndex)
1937 {
1938 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1939 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1940 mState.mSamplerBindings.emplace_back(
1941 SamplerBinding(textureType, samplerUniform.elementCount()));
1942 }
1943}
1944
Martin Radev4c4c8e72016-08-04 12:25:34 +03001945bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1946 const std::string &uniformName,
1947 const sh::InterfaceBlockField &vertexUniform,
1948 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001949{
Jamie Madillc4c744222015-11-04 09:39:47 -05001950 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1951 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001952 {
1953 return false;
1954 }
1955
1956 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1957 {
Jamie Madillf6113162015-05-07 11:49:21 -04001958 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001959 return false;
1960 }
1961
1962 return true;
1963}
1964
Jamie Madilleb979bf2016-11-15 12:28:46 -05001965// Assigns locations to all attributes from the bindings and program locations.
1966bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001967{
Jamie Madilleb979bf2016-11-15 12:28:46 -05001968 const auto *vertexShader = mState.getAttachedVertexShader();
1969
Geoff Lang7dd2e102014-11-10 15:19:26 -05001970 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001971 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001972 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001973
1974 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001975 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001976 {
Jamie Madillf6113162015-05-07 11:49:21 -04001977 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001978 return false;
1979 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001980
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001981 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001982
Jamie Madillc349ec02015-08-21 16:53:12 -04001983 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001984 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001985 {
1986 // TODO(jmadill): do staticUse filtering step here, or not at all
Geoff Lang7dd2e102014-11-10 15:19:26 -05001987 ASSERT(attribute.staticUse);
1988
Jamie Madilleb979bf2016-11-15 12:28:46 -05001989 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001990 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001991 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001992 attribute.location = bindingLocation;
1993 }
1994
1995 if (attribute.location != -1)
1996 {
1997 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001998 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001999
Jamie Madill63805b42015-08-25 13:17:39 -04002000 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001 {
Jamie Madillf6113162015-05-07 11:49:21 -04002002 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002003 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002004
2005 return false;
2006 }
2007
Jamie Madill63805b42015-08-25 13:17:39 -04002008 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002009 {
Jamie Madill63805b42015-08-25 13:17:39 -04002010 const int regLocation = attribute.location + reg;
2011 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012
2013 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002014 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002015 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002016 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002017 // TODO(jmadill): fix aliasing on ES2
2018 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002019 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002020 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002021 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002022 return false;
2023 }
2024 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002025 else
2026 {
Jamie Madill63805b42015-08-25 13:17:39 -04002027 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002028 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002029
Jamie Madill63805b42015-08-25 13:17:39 -04002030 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002031 }
2032 }
2033 }
2034
2035 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002036 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002037 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05002038 ASSERT(attribute.staticUse);
2039
Jamie Madillc349ec02015-08-21 16:53:12 -04002040 // Not set by glBindAttribLocation or by location layout qualifier
2041 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002042 {
Jamie Madill63805b42015-08-25 13:17:39 -04002043 int regs = VariableRegisterCount(attribute.type);
2044 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002045
Jamie Madill63805b42015-08-25 13:17:39 -04002046 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002047 {
Jamie Madillf6113162015-05-07 11:49:21 -04002048 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002049 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002050 }
2051
Jamie Madillc349ec02015-08-21 16:53:12 -04002052 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053 }
2054 }
2055
Jamie Madill48ef11b2016-04-27 15:21:52 -04002056 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002057 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002058 ASSERT(attribute.staticUse);
Jamie Madill63805b42015-08-25 13:17:39 -04002059 ASSERT(attribute.location != -1);
2060 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002061
Jamie Madill63805b42015-08-25 13:17:39 -04002062 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002063 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002064 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002065 }
2066 }
2067
Geoff Lang7dd2e102014-11-10 15:19:26 -05002068 return true;
2069}
2070
Martin Radev4c4c8e72016-08-04 12:25:34 +03002071bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2072 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2073 const std::string &errorMessage,
2074 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002076 GLuint blockCount = 0;
2077 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002078 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002079 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002080 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002081 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002082 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002083 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002084 return false;
2085 }
2086 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002087 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002088 return true;
2089}
Jamie Madille473dee2015-08-18 14:49:01 -04002090
Martin Radev4c4c8e72016-08-04 12:25:34 +03002091bool Program::validateVertexAndFragmentInterfaceBlocks(
2092 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2093 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2094 InfoLog &infoLog) const
2095{
2096 // Check that interface blocks defined in the vertex and fragment shaders are identical
2097 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2098 UniformBlockMap linkedUniformBlocks;
2099
2100 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2101 {
2102 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2103 }
2104
Jamie Madille473dee2015-08-18 14:49:01 -04002105 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002106 {
Jamie Madille473dee2015-08-18 14:49:01 -04002107 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002108 if (entry != linkedUniformBlocks.end())
2109 {
2110 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2111 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2112 {
2113 return false;
2114 }
2115 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002116 }
2117 return true;
2118}
Jamie Madille473dee2015-08-18 14:49:01 -04002119
Martin Radev4c4c8e72016-08-04 12:25:34 +03002120bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2121{
2122 if (mState.mAttachedComputeShader)
2123 {
2124 const Shader &computeShader = *mState.mAttachedComputeShader;
2125 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2126
2127 if (!validateUniformBlocksCount(
2128 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2129 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2130 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002131 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002132 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002133 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002134 return true;
2135 }
2136
2137 const Shader &vertexShader = *mState.mAttachedVertexShader;
2138 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2139
2140 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2141 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2142
2143 if (!validateUniformBlocksCount(
2144 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2145 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2146 {
2147 return false;
2148 }
2149 if (!validateUniformBlocksCount(
2150 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2151 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2152 infoLog))
2153 {
2154
2155 return false;
2156 }
2157 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2158 infoLog))
2159 {
2160 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002161 }
Jamie Madille473dee2015-08-18 14:49:01 -04002162
Geoff Lang7dd2e102014-11-10 15:19:26 -05002163 return true;
2164}
2165
Jamie Madilla2c74982016-12-12 11:20:42 -05002166bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002167 const sh::InterfaceBlock &vertexInterfaceBlock,
2168 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002169{
2170 const char* blockName = vertexInterfaceBlock.name.c_str();
2171 // validate blocks for the same member types
2172 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2173 {
Jamie Madillf6113162015-05-07 11:49:21 -04002174 infoLog << "Types for interface block '" << blockName
2175 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002176 return false;
2177 }
2178 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2179 {
Jamie Madillf6113162015-05-07 11:49:21 -04002180 infoLog << "Array sizes differ for interface block '" << blockName
2181 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002182 return false;
2183 }
2184 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2185 {
Jamie Madillf6113162015-05-07 11:49:21 -04002186 infoLog << "Layout qualifiers differ for interface block '" << blockName
2187 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002188 return false;
2189 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002190 const unsigned int numBlockMembers =
2191 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002192 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2193 {
2194 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2195 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2196 if (vertexMember.name != fragmentMember.name)
2197 {
Jamie Madillf6113162015-05-07 11:49:21 -04002198 infoLog << "Name mismatch for field " << blockMemberIndex
2199 << " of interface block '" << blockName
2200 << "': (in vertex: '" << vertexMember.name
2201 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002202 return false;
2203 }
2204 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2205 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2206 {
2207 return false;
2208 }
2209 }
2210 return true;
2211}
2212
2213bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2214 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2215{
2216 if (vertexVariable.type != fragmentVariable.type)
2217 {
Jamie Madillf6113162015-05-07 11:49:21 -04002218 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002219 return false;
2220 }
2221 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2222 {
Jamie Madillf6113162015-05-07 11:49:21 -04002223 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002224 return false;
2225 }
2226 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2227 {
Jamie Madillf6113162015-05-07 11:49:21 -04002228 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002229 return false;
2230 }
2231
2232 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2233 {
Jamie Madillf6113162015-05-07 11:49:21 -04002234 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002235 return false;
2236 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002237 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002238 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2239 {
2240 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2241 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2242
2243 if (vertexMember.name != fragmentMember.name)
2244 {
Jamie Madillf6113162015-05-07 11:49:21 -04002245 infoLog << "Name mismatch for field '" << memberIndex
2246 << "' of " << variableName
2247 << ": (in vertex: '" << vertexMember.name
2248 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002249 return false;
2250 }
2251
2252 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2253 vertexMember.name + "'";
2254
2255 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2256 {
2257 return false;
2258 }
2259 }
2260
2261 return true;
2262}
2263
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002264bool Program::linkValidateVaryings(InfoLog &infoLog,
2265 const std::string &varyingName,
2266 const sh::Varying &vertexVarying,
2267 const sh::Varying &fragmentVarying,
2268 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002269{
2270 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2271 {
2272 return false;
2273 }
2274
Jamie Madille9cc4692015-02-19 16:00:13 -05002275 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002276 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002277 infoLog << "Interpolation types for " << varyingName
2278 << " differ between vertex and fragment shaders.";
2279 return false;
2280 }
2281
2282 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2283 {
2284 infoLog << "Invariance for " << varyingName
2285 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002286 return false;
2287 }
2288
2289 return true;
2290}
2291
Yuly Novikov817232e2017-02-22 18:36:10 -05002292bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
2293{
2294 const Shader *vertexShader = mState.mAttachedVertexShader;
2295 const Shader *fragmentShader = mState.mAttachedFragmentShader;
2296 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
2297 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
2298 int shaderVersion = vertexShader->getShaderVersion();
2299
2300 if (shaderVersion != 100)
2301 {
2302 // Only ESSL 1.0 has restrictions on matching input and output invariance
2303 return true;
2304 }
2305
2306 bool glPositionIsInvariant = false;
2307 bool glPointSizeIsInvariant = false;
2308 bool glFragCoordIsInvariant = false;
2309 bool glPointCoordIsInvariant = false;
2310
2311 for (const sh::Varying &varying : vertexVaryings)
2312 {
2313 if (!varying.isBuiltIn())
2314 {
2315 continue;
2316 }
2317 if (varying.name.compare("gl_Position") == 0)
2318 {
2319 glPositionIsInvariant = varying.isInvariant;
2320 }
2321 else if (varying.name.compare("gl_PointSize") == 0)
2322 {
2323 glPointSizeIsInvariant = varying.isInvariant;
2324 }
2325 }
2326
2327 for (const sh::Varying &varying : fragmentVaryings)
2328 {
2329 if (!varying.isBuiltIn())
2330 {
2331 continue;
2332 }
2333 if (varying.name.compare("gl_FragCoord") == 0)
2334 {
2335 glFragCoordIsInvariant = varying.isInvariant;
2336 }
2337 else if (varying.name.compare("gl_PointCoord") == 0)
2338 {
2339 glPointCoordIsInvariant = varying.isInvariant;
2340 }
2341 }
2342
2343 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2344 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2345 // Not requiring invariance to match is supported by:
2346 // dEQP, WebGL CTS, Nexus 5X GLES
2347 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2348 {
2349 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2350 "declared invariant.";
2351 return false;
2352 }
2353 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2354 {
2355 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2356 "declared invariant.";
2357 return false;
2358 }
2359
2360 return true;
2361}
2362
Jamie Madillccdf74b2015-08-18 10:46:12 -04002363bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002364 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002365 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002366{
2367 size_t totalComponents = 0;
2368
Jamie Madillccdf74b2015-08-18 10:46:12 -04002369 std::set<std::string> uniqueNames;
2370
Jamie Madill48ef11b2016-04-27 15:21:52 -04002371 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002372 {
2373 bool found = false;
Jamie Madill192745a2016-12-22 15:58:21 -05002374 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002375 {
Jamie Madill192745a2016-12-22 15:58:21 -05002376 const sh::Varying *varying = ref.second.get();
2377
Jamie Madillccdf74b2015-08-18 10:46:12 -04002378 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002379 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002380 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002381 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002382 infoLog << "Two transform feedback varyings specify the same output variable ("
2383 << tfVaryingName << ").";
2384 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002385 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002386 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002387
Geoff Lang1a683462015-09-29 15:09:59 -04002388 if (varying->isArray())
2389 {
2390 infoLog << "Capture of arrays is undefined and not supported.";
2391 return false;
2392 }
2393
Jamie Madillccdf74b2015-08-18 10:46:12 -04002394 // TODO(jmadill): Investigate implementation limits on D3D11
Jamie Madilla2c74982016-12-12 11:20:42 -05002395 size_t componentCount = VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002396 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002397 componentCount > caps.maxTransformFeedbackSeparateComponents)
2398 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002399 infoLog << "Transform feedback varying's " << varying->name << " components ("
2400 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002401 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002402 return false;
2403 }
2404
2405 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002406 found = true;
2407 break;
2408 }
2409 }
2410
Jamie Madill89bb70e2015-08-31 14:18:39 -04002411 if (tfVaryingName.find('[') != std::string::npos)
2412 {
Geoff Lang1a683462015-09-29 15:09:59 -04002413 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002414 return false;
2415 }
2416
Geoff Lang7dd2e102014-11-10 15:19:26 -05002417 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2418 ASSERT(found);
2419 }
2420
Jamie Madill48ef11b2016-04-27 15:21:52 -04002421 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002422 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002423 {
Jamie Madillf6113162015-05-07 11:49:21 -04002424 infoLog << "Transform feedback varying total components (" << totalComponents
2425 << ") exceed the maximum interleaved components ("
2426 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002427 return false;
2428 }
2429
2430 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002431}
2432
Jamie Madill192745a2016-12-22 15:58:21 -05002433void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002434{
2435 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002436 mState.mTransformFeedbackVaryingVars.clear();
2437 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002438 {
Jamie Madill192745a2016-12-22 15:58:21 -05002439 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002440 {
Jamie Madill192745a2016-12-22 15:58:21 -05002441 const sh::Varying *varying = ref.second.get();
Jamie Madillccdf74b2015-08-18 10:46:12 -04002442 if (tfVaryingName == varying->name)
2443 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002444 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002445 break;
2446 }
2447 }
2448 }
2449}
2450
Jamie Madill192745a2016-12-22 15:58:21 -05002451Program::MergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002452{
Jamie Madill192745a2016-12-22 15:58:21 -05002453 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002454
Jamie Madill48ef11b2016-04-27 15:21:52 -04002455 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002456 {
Jamie Madill192745a2016-12-22 15:58:21 -05002457 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002458 }
2459
Jamie Madill48ef11b2016-04-27 15:21:52 -04002460 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002461 {
Jamie Madill192745a2016-12-22 15:58:21 -05002462 merged[varying.name].fragment = &varying;
2463 }
2464
2465 return merged;
2466}
2467
2468std::vector<PackedVarying> Program::getPackedVaryings(
2469 const Program::MergedVaryings &mergedVaryings) const
2470{
2471 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2472 std::vector<PackedVarying> packedVaryings;
2473
2474 for (const auto &ref : mergedVaryings)
2475 {
2476 const sh::Varying *input = ref.second.vertex;
2477 const sh::Varying *output = ref.second.fragment;
2478
2479 // Only pack varyings that have a matched input or output, plus special builtins.
2480 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002481 {
Jamie Madill192745a2016-12-22 15:58:21 -05002482 // Will get the vertex shader interpolation by default.
2483 auto interpolation = ref.second.get()->interpolation;
2484
2485 // Interpolation qualifiers must match.
2486 if (output->isStruct())
2487 {
2488 ASSERT(!output->isArray());
2489 for (const auto &field : output->fields)
2490 {
2491 ASSERT(!field.isStruct() && !field.isArray());
2492 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2493 }
2494 }
2495 else
2496 {
2497 packedVaryings.push_back(PackedVarying(*output, interpolation));
2498 }
2499 continue;
2500 }
2501
2502 // Keep Transform FB varyings in the merged list always.
2503 if (!input)
2504 {
2505 continue;
2506 }
2507
2508 for (const std::string &tfVarying : tfVaryings)
2509 {
2510 if (tfVarying == input->name)
2511 {
2512 // Transform feedback for varying structs is underspecified.
2513 // See Khronos bug 9856.
2514 // TODO(jmadill): Figure out how to be spec-compliant here.
2515 if (!input->isStruct())
2516 {
2517 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2518 packedVaryings.back().vertexOnly = true;
2519 }
2520 break;
2521 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002522 }
2523 }
2524
Jamie Madill192745a2016-12-22 15:58:21 -05002525 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2526
2527 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002528}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002529
2530void Program::linkOutputVariables()
2531{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002532 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002533 ASSERT(fragmentShader != nullptr);
2534
2535 // Skip this step for GLES2 shaders.
2536 if (fragmentShader->getShaderVersion() == 100)
2537 return;
2538
Jamie Madilla0a9e122015-09-02 15:54:30 -04002539 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002540
2541 // TODO(jmadill): any caps validation here?
2542
2543 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2544 outputVariableIndex++)
2545 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002546 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002547
2548 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2549 if (outputVariable.isBuiltIn())
2550 continue;
2551
2552 // Since multiple output locations must be specified, use 0 for non-specified locations.
2553 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2554
2555 ASSERT(outputVariable.staticUse);
2556
2557 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2558 elementIndex++)
2559 {
2560 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002561 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002562 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002563 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002564 VariableLocation(outputVariable.name, element, outputVariableIndex);
2565 }
2566 }
2567}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002568
Jamie Madill62d31cb2015-09-11 13:25:51 -04002569void Program::gatherInterfaceBlockInfo()
2570{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002571 ASSERT(mState.mUniformBlocks.empty());
2572
2573 if (mState.mAttachedComputeShader)
2574 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002575 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002576
2577 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2578 {
2579
2580 // Only 'packed' blocks are allowed to be considered inactive.
2581 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2582 continue;
2583
Jamie Madilla2c74982016-12-12 11:20:42 -05002584 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002585 {
2586 if (block.name == computeBlock.name)
2587 {
2588 block.computeStaticUse = computeBlock.staticUse;
2589 }
2590 }
2591
2592 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2593 }
2594 return;
2595 }
2596
Jamie Madill62d31cb2015-09-11 13:25:51 -04002597 std::set<std::string> visitedList;
2598
Jamie Madilla2c74982016-12-12 11:20:42 -05002599 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002600
Jamie Madill62d31cb2015-09-11 13:25:51 -04002601 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2602 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002603 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002604 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2605 continue;
2606
2607 if (visitedList.count(vertexBlock.name) > 0)
2608 continue;
2609
2610 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2611 visitedList.insert(vertexBlock.name);
2612 }
2613
Jamie Madilla2c74982016-12-12 11:20:42 -05002614 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002615
2616 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2617 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002618 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002619 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2620 continue;
2621
2622 if (visitedList.count(fragmentBlock.name) > 0)
2623 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002624 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002625 {
2626 if (block.name == fragmentBlock.name)
2627 {
2628 block.fragmentStaticUse = fragmentBlock.staticUse;
2629 }
2630 }
2631
2632 continue;
2633 }
2634
2635 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2636 visitedList.insert(fragmentBlock.name);
2637 }
2638}
2639
Jamie Madill4a3c2342015-10-08 12:58:45 -04002640template <typename VarT>
2641void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2642 const std::string &prefix,
2643 int blockIndex)
2644{
2645 for (const VarT &field : fields)
2646 {
2647 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2648
2649 if (field.isStruct())
2650 {
2651 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2652 {
2653 const std::string uniformElementName =
2654 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2655 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2656 }
2657 }
2658 else
2659 {
2660 // If getBlockMemberInfo returns false, the uniform is optimized out.
2661 sh::BlockMemberInfo memberInfo;
2662 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2663 {
2664 continue;
2665 }
2666
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002667 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002668 blockIndex, memberInfo);
2669
2670 // Since block uniforms have no location, we don't need to store them in the uniform
2671 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002672 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002673 }
2674 }
2675}
2676
Jamie Madill62d31cb2015-09-11 13:25:51 -04002677void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2678{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002679 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002680 size_t blockSize = 0;
2681
2682 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002683 std::stringstream blockNameStr;
2684 blockNameStr << interfaceBlock.name;
2685 if (interfaceBlock.arraySize > 0)
2686 {
2687 blockNameStr << "[0]";
2688 }
2689 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002690 {
2691 return;
2692 }
2693
2694 // Track the first and last uniform index to determine the range of active uniforms in the
2695 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002696 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002697 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002698 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002699
2700 std::vector<unsigned int> blockUniformIndexes;
2701 for (size_t blockUniformIndex = firstBlockUniformIndex;
2702 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2703 {
2704 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2705 }
2706
2707 if (interfaceBlock.arraySize > 0)
2708 {
2709 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2710 {
2711 UniformBlock block(interfaceBlock.name, true, arrayElement);
2712 block.memberUniformIndexes = blockUniformIndexes;
2713
Martin Radev4c4c8e72016-08-04 12:25:34 +03002714 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002715 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002716 case GL_VERTEX_SHADER:
2717 {
2718 block.vertexStaticUse = interfaceBlock.staticUse;
2719 break;
2720 }
2721 case GL_FRAGMENT_SHADER:
2722 {
2723 block.fragmentStaticUse = interfaceBlock.staticUse;
2724 break;
2725 }
2726 case GL_COMPUTE_SHADER:
2727 {
2728 block.computeStaticUse = interfaceBlock.staticUse;
2729 break;
2730 }
2731 default:
2732 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002733 }
2734
Qin Jiajia0350a642016-11-01 17:01:51 +08002735 // Since all block elements in an array share the same active uniforms, they will all be
2736 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2737 // here we will add every block element in the array.
2738 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002739 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002740 }
2741 }
2742 else
2743 {
2744 UniformBlock block(interfaceBlock.name, false, 0);
2745 block.memberUniformIndexes = blockUniformIndexes;
2746
Martin Radev4c4c8e72016-08-04 12:25:34 +03002747 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002748 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002749 case GL_VERTEX_SHADER:
2750 {
2751 block.vertexStaticUse = interfaceBlock.staticUse;
2752 break;
2753 }
2754 case GL_FRAGMENT_SHADER:
2755 {
2756 block.fragmentStaticUse = interfaceBlock.staticUse;
2757 break;
2758 }
2759 case GL_COMPUTE_SHADER:
2760 {
2761 block.computeStaticUse = interfaceBlock.staticUse;
2762 break;
2763 }
2764 default:
2765 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002766 }
2767
Jamie Madill4a3c2342015-10-08 12:58:45 -04002768 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002769 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002770 }
2771}
2772
Jamie Madille7d84322017-01-10 18:21:59 -05002773template <>
2774void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2775 const uint8_t *destPointer,
2776 GLsizei clampedCount,
2777 const GLint *v)
2778{
2779 // Invalidate the validation cache only if we modify the sampler data.
2780 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2781 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2782 {
2783 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2784 std::vector<GLuint> *boundTextureUnits =
2785 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2786
2787 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2788 mCachedValidateSamplersResult.reset();
2789 }
2790}
2791
2792template <typename T>
2793void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2794 const uint8_t *destPointer,
2795 GLsizei clampedCount,
2796 const T *v)
2797{
2798}
2799
Jamie Madill62d31cb2015-09-11 13:25:51 -04002800template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002801GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002802{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002803 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2804 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002805 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2806
Corentin Wallez15ac5342016-11-03 17:06:39 -04002807 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2808 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2809 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002810 GLsizei maxElementCount =
2811 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2812
2813 GLsizei count = countIn;
2814 GLsizei clampedCount = count * vectorSize;
2815 if (clampedCount > maxElementCount)
2816 {
2817 clampedCount = maxElementCount;
2818 count = maxElementCount / vectorSize;
2819 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002820
Jamie Madill62d31cb2015-09-11 13:25:51 -04002821 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2822 {
2823 // Do a cast conversion for boolean types. From the spec:
2824 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2825 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002826 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002827 {
2828 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2829 }
2830 }
2831 else
2832 {
Jamie Madille7d84322017-01-10 18:21:59 -05002833 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002834 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002835 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002836
2837 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002838}
2839
2840template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002841GLsizei Program::setMatrixUniformInternal(GLint location,
2842 GLsizei count,
2843 GLboolean transpose,
2844 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002845{
2846 if (!transpose)
2847 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002848 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002849 }
2850
2851 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002852 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2853 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002854 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002855
2856 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2857 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2858 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2859 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2860
2861 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002862 {
2863 size_t elementOffset = element * rows * cols;
2864
2865 for (size_t row = 0; row < rows; ++row)
2866 {
2867 for (size_t col = 0; col < cols; ++col)
2868 {
2869 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2870 }
2871 }
2872 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002873
2874 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002875}
2876
2877template <typename DestT>
2878void Program::getUniformInternal(GLint location, DestT *dataOut) const
2879{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002880 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2881 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002882
2883 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2884
2885 GLenum componentType = VariableComponentType(uniform.type);
2886 if (componentType == GLTypeToGLenum<DestT>::value)
2887 {
2888 memcpy(dataOut, srcPointer, uniform.getElementSize());
2889 return;
2890 }
2891
Corentin Wallez6596c462016-03-17 17:26:58 -04002892 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002893
2894 switch (componentType)
2895 {
2896 case GL_INT:
2897 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2898 break;
2899 case GL_UNSIGNED_INT:
2900 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2901 break;
2902 case GL_BOOL:
2903 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2904 break;
2905 case GL_FLOAT:
2906 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2907 break;
2908 default:
2909 UNREACHABLE();
2910 }
2911}
Jamie Madilla4595b82017-01-11 17:36:34 -05002912
2913bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2914{
2915 // Must be called after samplers are validated.
2916 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2917
2918 for (const auto &binding : mState.mSamplerBindings)
2919 {
2920 GLenum textureType = binding.textureType;
2921 for (const auto &unit : binding.boundTextureUnits)
2922 {
2923 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2924 if (programTextureID == textureID)
2925 {
2926 // TODO(jmadill): Check for appropriate overlap.
2927 return true;
2928 }
2929 }
2930 }
2931
2932 return false;
2933}
2934
Jamie Madilla2c74982016-12-12 11:20:42 -05002935} // namespace gl