blob: 4770a8d105f8e639a93cdf2336cda27110e1aca2 [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 {
jchen1036e120e2017-03-14 14:53:58 +08001143 if (attribute.name == name)
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
jchen1036e120e2017-03-14 14:53:58 +08001177 ASSERT(index < mState.mAttributes.size());
1178 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001179
1180 if (bufsize > 0)
1181 {
1182 const char *string = attrib.name.c_str();
1183
1184 strncpy(name, string, bufsize);
1185 name[bufsize - 1] = '\0';
1186
1187 if (length)
1188 {
1189 *length = static_cast<GLsizei>(strlen(name));
1190 }
1191 }
1192
1193 // Always a single 'type' instance
1194 *size = 1;
1195 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001196}
1197
Geoff Lange1a27752015-10-05 13:16:04 -04001198GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001199{
Jamie Madillc349ec02015-08-21 16:53:12 -04001200 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001201 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001202 return 0;
1203 }
1204
jchen1036e120e2017-03-14 14:53:58 +08001205 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001206}
1207
Geoff Lange1a27752015-10-05 13:16:04 -04001208GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001209{
Jamie Madillc349ec02015-08-21 16:53:12 -04001210 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001211 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001212 return 0;
1213 }
1214
1215 size_t maxLength = 0;
1216
Jamie Madill48ef11b2016-04-27 15:21:52 -04001217 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001218 {
jchen1036e120e2017-03-14 14:53:58 +08001219 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001220 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001221
Jamie Madillc349ec02015-08-21 16:53:12 -04001222 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001223}
1224
Geoff Lang7dd2e102014-11-10 15:19:26 -05001225GLint Program::getFragDataLocation(const std::string &name) const
1226{
1227 std::string baseName(name);
1228 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001229 for (auto outputPair : mState.mOutputVariables)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001230 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001231 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001232 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1233 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001234 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001235 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001236 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001237 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001238}
1239
Geoff Lange1a27752015-10-05 13:16:04 -04001240void Program::getActiveUniform(GLuint index,
1241 GLsizei bufsize,
1242 GLsizei *length,
1243 GLint *size,
1244 GLenum *type,
1245 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001246{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001247 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001248 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001249 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001250 ASSERT(index < mState.mUniforms.size());
1251 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001252
1253 if (bufsize > 0)
1254 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001255 std::string string = uniform.name;
1256 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001257 {
1258 string += "[0]";
1259 }
1260
1261 strncpy(name, string.c_str(), bufsize);
1262 name[bufsize - 1] = '\0';
1263
1264 if (length)
1265 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001266 *length = static_cast<GLsizei>(strlen(name));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001267 }
1268 }
1269
Jamie Madill62d31cb2015-09-11 13:25:51 -04001270 *size = uniform.elementCount();
1271 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001272 }
1273 else
1274 {
1275 if (bufsize > 0)
1276 {
1277 name[0] = '\0';
1278 }
1279
1280 if (length)
1281 {
1282 *length = 0;
1283 }
1284
1285 *size = 0;
1286 *type = GL_NONE;
1287 }
1288}
1289
Geoff Lange1a27752015-10-05 13:16:04 -04001290GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001291{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001292 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001293 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001294 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001295 }
1296 else
1297 {
1298 return 0;
1299 }
1300}
1301
Geoff Lange1a27752015-10-05 13:16:04 -04001302GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001303{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001304 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001305
1306 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001307 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001308 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001309 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001310 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001311 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001312 size_t length = uniform.name.length() + 1u;
1313 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001314 {
1315 length += 3; // Counting in "[0]".
1316 }
1317 maxLength = std::max(length, maxLength);
1318 }
1319 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001320 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001321
Jamie Madill62d31cb2015-09-11 13:25:51 -04001322 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001323}
1324
1325GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1326{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001327 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001328 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001329 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001330 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001331 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1332 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1333 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1334 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1335 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1336 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1337 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1338 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1339 default:
1340 UNREACHABLE();
1341 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001342 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001343 return 0;
1344}
1345
1346bool Program::isValidUniformLocation(GLint location) const
1347{
Jamie Madille2e406c2016-06-02 13:04:10 -04001348 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001349 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1350 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001351}
1352
Jamie Madill62d31cb2015-09-11 13:25:51 -04001353const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001354{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001355 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001356 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001357}
1358
Jamie Madillac4e9c32017-01-13 14:07:12 -05001359const VariableLocation &Program::getUniformLocation(GLint location) const
1360{
1361 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1362 return mState.mUniformLocations[location];
1363}
1364
1365const std::vector<VariableLocation> &Program::getUniformLocations() const
1366{
1367 return mState.mUniformLocations;
1368}
1369
1370const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1371{
1372 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1373 return mState.mUniforms[index];
1374}
1375
Jamie Madill62d31cb2015-09-11 13:25:51 -04001376GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001377{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001378 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001379}
1380
Jamie Madill62d31cb2015-09-11 13:25:51 -04001381GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001382{
Jamie Madille7d84322017-01-10 18:21:59 -05001383 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001384}
1385
1386void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1387{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001388 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1389 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001390}
1391
1392void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1393{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001394 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1395 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001396}
1397
1398void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1399{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001400 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1401 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001402}
1403
1404void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1405{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001406 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1407 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001408}
1409
1410void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1411{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001412 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1413 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414}
1415
1416void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1417{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001418 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1419 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001420}
1421
1422void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1423{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001424 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1425 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426}
1427
1428void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1429{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001430 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1431 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001432}
1433
1434void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1435{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001436 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1437 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001438}
1439
1440void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1441{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001442 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1443 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001444}
1445
1446void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1447{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001448 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1449 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001450}
1451
1452void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1453{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001454 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1455 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001456}
1457
1458void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1459{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001460 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1461 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462}
1463
1464void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1465{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001466 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1467 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001468}
1469
1470void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1471{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001472 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1473 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001474}
1475
1476void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1477{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001478 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1479 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001480}
1481
1482void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1483{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001484 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1485 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001486}
1487
1488void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1489{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001490 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1491 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001492}
1493
1494void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1495{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001496 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1497 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001498}
1499
1500void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1501{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001502 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1503 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001504}
1505
1506void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1507{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001508 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1509 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001510}
1511
Geoff Lange1a27752015-10-05 13:16:04 -04001512void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001514 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001515}
1516
Geoff Lange1a27752015-10-05 13:16:04 -04001517void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001518{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001519 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001520}
1521
Geoff Lange1a27752015-10-05 13:16:04 -04001522void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001523{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001524 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525}
1526
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001527void Program::flagForDeletion()
1528{
1529 mDeleteStatus = true;
1530}
1531
1532bool Program::isFlaggedForDeletion() const
1533{
1534 return mDeleteStatus;
1535}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001536
Brandon Jones43a53e22014-08-28 16:23:22 -07001537void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001538{
1539 mInfoLog.reset();
1540
Geoff Lang7dd2e102014-11-10 15:19:26 -05001541 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001542 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001543 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001544 }
1545 else
1546 {
Jamie Madillf6113162015-05-07 11:49:21 -04001547 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001548 }
1549}
1550
Geoff Lang7dd2e102014-11-10 15:19:26 -05001551bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1552{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001553 // Skip cache if we're using an infolog, so we get the full error.
1554 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1555 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1556 {
1557 return mCachedValidateSamplersResult.value();
1558 }
1559
1560 if (mTextureUnitTypesCache.empty())
1561 {
1562 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1563 }
1564 else
1565 {
1566 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1567 }
1568
1569 // if any two active samplers in a program are of different types, but refer to the same
1570 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1571 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001572 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001573 {
Jamie Madille7d84322017-01-10 18:21:59 -05001574 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001575
Jamie Madille7d84322017-01-10 18:21:59 -05001576 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001577 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001578 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1579 {
1580 if (infoLog)
1581 {
1582 (*infoLog) << "Sampler uniform (" << textureUnit
1583 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1584 << caps.maxCombinedTextureImageUnits << ")";
1585 }
1586
1587 mCachedValidateSamplersResult = false;
1588 return false;
1589 }
1590
1591 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1592 {
1593 if (textureType != mTextureUnitTypesCache[textureUnit])
1594 {
1595 if (infoLog)
1596 {
1597 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1598 "image unit ("
1599 << textureUnit << ").";
1600 }
1601
1602 mCachedValidateSamplersResult = false;
1603 return false;
1604 }
1605 }
1606 else
1607 {
1608 mTextureUnitTypesCache[textureUnit] = textureType;
1609 }
1610 }
1611 }
1612
1613 mCachedValidateSamplersResult = true;
1614 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001615}
1616
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001617bool Program::isValidated() const
1618{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001619 return mValidated;
1620}
1621
Geoff Lange1a27752015-10-05 13:16:04 -04001622GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001623{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001624 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625}
1626
1627void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1628{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001629 ASSERT(
1630 uniformBlockIndex <
1631 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001632
Jamie Madill48ef11b2016-04-27 15:21:52 -04001633 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001634
1635 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001636 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001637 std::string string = uniformBlock.name;
1638
Jamie Madill62d31cb2015-09-11 13:25:51 -04001639 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001640 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001641 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001642 }
1643
1644 strncpy(uniformBlockName, string.c_str(), bufSize);
1645 uniformBlockName[bufSize - 1] = '\0';
1646
1647 if (length)
1648 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001649 *length = static_cast<GLsizei>(strlen(uniformBlockName));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001650 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001651 }
1652}
1653
Geoff Lange1a27752015-10-05 13:16:04 -04001654GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001655{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001656 int maxLength = 0;
1657
1658 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001659 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001660 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001661 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1662 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001663 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001664 if (!uniformBlock.name.empty())
1665 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001666 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001667
1668 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001669 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001670
1671 maxLength = std::max(length + arrayLength, maxLength);
1672 }
1673 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001674 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001675
1676 return maxLength;
1677}
1678
Geoff Lange1a27752015-10-05 13:16:04 -04001679GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001680{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001681 size_t subscript = GL_INVALID_INDEX;
Jamie Madilla2c74982016-12-12 11:20:42 -05001682 std::string baseName = ParseUniformName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001683
Jamie Madill48ef11b2016-04-27 15:21:52 -04001684 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001685 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1686 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001687 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001688 if (uniformBlock.name == baseName)
1689 {
1690 const bool arrayElementZero =
1691 (subscript == GL_INVALID_INDEX &&
1692 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1693 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1694 {
1695 return blockIndex;
1696 }
1697 }
1698 }
1699
1700 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001701}
1702
Jamie Madill62d31cb2015-09-11 13:25:51 -04001703const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001704{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001705 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1706 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001707}
1708
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001709void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1710{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001711 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001712 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001713 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001714}
1715
1716GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1717{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001718 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001719}
1720
1721void Program::resetUniformBlockBindings()
1722{
1723 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1724 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001725 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001726 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001727 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001728}
1729
Geoff Lang48dcae72014-02-05 16:28:24 -05001730void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1731{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001732 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001733 for (GLsizei i = 0; i < count; i++)
1734 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001735 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001736 }
1737
Jamie Madill48ef11b2016-04-27 15:21:52 -04001738 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001739}
1740
1741void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1742{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001743 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001744 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001745 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1746 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001747 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1748 if (length)
1749 {
1750 *length = lastNameIdx;
1751 }
1752 if (size)
1753 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001754 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001755 }
1756 if (type)
1757 {
1758 *type = varying.type;
1759 }
1760 if (name)
1761 {
1762 memcpy(name, varying.name.c_str(), lastNameIdx);
1763 name[lastNameIdx] = '\0';
1764 }
1765 }
1766}
1767
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001768GLsizei Program::getTransformFeedbackVaryingCount() const
1769{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001770 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001771 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001772 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001773 }
1774 else
1775 {
1776 return 0;
1777 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001778}
1779
1780GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1781{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001782 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001783 {
1784 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001785 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001786 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001787 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1788 }
1789
1790 return maxSize;
1791 }
1792 else
1793 {
1794 return 0;
1795 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001796}
1797
1798GLenum Program::getTransformFeedbackBufferMode() const
1799{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001800 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001801}
1802
Jamie Madill192745a2016-12-22 15:58:21 -05001803bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001804{
Jamie Madill192745a2016-12-22 15:58:21 -05001805 const Shader *vertexShader = mState.mAttachedVertexShader;
1806 const Shader *fragmentShader = mState.mAttachedFragmentShader;
1807
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001808 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1809
Jamie Madill4cff2472015-08-21 16:53:18 -04001810 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1811 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001812
Sami Väisänen46eaa942016-06-29 10:26:37 +03001813 std::map<GLuint, std::string> staticFragmentInputLocations;
1814
Jamie Madill4cff2472015-08-21 16:53:18 -04001815 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001816 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001817 bool matched = false;
1818
1819 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001820 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821 {
1822 continue;
1823 }
1824
Jamie Madill4cff2472015-08-21 16:53:18 -04001825 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001827 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001828 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001829 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001830 if (!linkValidateVaryings(infoLog, output.name, input, output,
1831 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001832 {
1833 return false;
1834 }
1835
Geoff Lang7dd2e102014-11-10 15:19:26 -05001836 matched = true;
1837 break;
1838 }
1839 }
1840
1841 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001842 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001843 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001844 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001845 return false;
1846 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001847
1848 // Check for aliased path rendering input bindings (if any).
1849 // If more than one binding refer statically to the same
1850 // location the link must fail.
1851
1852 if (!output.staticUse)
1853 continue;
1854
1855 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1856 if (inputBinding == -1)
1857 continue;
1858
1859 const auto it = staticFragmentInputLocations.find(inputBinding);
1860 if (it == std::end(staticFragmentInputLocations))
1861 {
1862 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1863 }
1864 else
1865 {
1866 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1867 << it->second;
1868 return false;
1869 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001870 }
1871
Yuly Novikov817232e2017-02-22 18:36:10 -05001872 if (!linkValidateBuiltInVaryings(infoLog))
1873 {
1874 return false;
1875 }
1876
Jamie Madillada9ecc2015-08-17 12:53:37 -04001877 // TODO(jmadill): verify no unmatched vertex varyings?
1878
Geoff Lang7dd2e102014-11-10 15:19:26 -05001879 return true;
1880}
1881
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001882bool Program::linkUniforms(InfoLog &infoLog,
1883 const Caps &caps,
1884 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001885{
Olli Etuahob78707c2017-03-09 15:03:11 +00001886 UniformLinker linker(mState);
1887 if (!linker.link(infoLog, caps, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001888 {
1889 return false;
1890 }
1891
Olli Etuahob78707c2017-03-09 15:03:11 +00001892 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001893
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001894 updateSamplerBindings();
1895
1896 return true;
1897}
1898
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001899void Program::updateSamplerBindings()
1900{
1901 mState.mSamplerUniformRange.end = static_cast<unsigned int>(mState.mUniforms.size());
1902 mState.mSamplerUniformRange.start = mState.mSamplerUniformRange.end;
1903 auto samplerIter = mState.mUniforms.rbegin();
1904 while (samplerIter != mState.mUniforms.rend() && samplerIter->isSampler())
1905 {
1906 --mState.mSamplerUniformRange.start;
1907 ++samplerIter;
1908 }
1909 // If uniform is a sampler type, insert it into the mSamplerBindings array.
1910 for (unsigned int samplerIndex = mState.mSamplerUniformRange.start;
1911 samplerIndex < mState.mUniforms.size(); ++samplerIndex)
1912 {
1913 const auto &samplerUniform = mState.mUniforms[samplerIndex];
1914 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
1915 mState.mSamplerBindings.emplace_back(
1916 SamplerBinding(textureType, samplerUniform.elementCount()));
1917 }
1918}
1919
Martin Radev4c4c8e72016-08-04 12:25:34 +03001920bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
1921 const std::string &uniformName,
1922 const sh::InterfaceBlockField &vertexUniform,
1923 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001924{
Jamie Madillc4c744222015-11-04 09:39:47 -05001925 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
1926 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001927 {
1928 return false;
1929 }
1930
1931 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
1932 {
Jamie Madillf6113162015-05-07 11:49:21 -04001933 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934 return false;
1935 }
1936
1937 return true;
1938}
1939
Jamie Madilleb979bf2016-11-15 12:28:46 -05001940// Assigns locations to all attributes from the bindings and program locations.
1941bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001942{
Jamie Madilleb979bf2016-11-15 12:28:46 -05001943 const auto *vertexShader = mState.getAttachedVertexShader();
1944
Geoff Lang7dd2e102014-11-10 15:19:26 -05001945 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001946 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001947 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04001948
1949 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04001950 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04001951 {
Jamie Madillf6113162015-05-07 11:49:21 -04001952 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04001953 return false;
1954 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001955
Jamie Madilldfde6ab2016-06-09 07:07:18 -07001956 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00001957
Jamie Madillc349ec02015-08-21 16:53:12 -04001958 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04001959 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001960 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05001961 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04001962 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04001963 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001964 attribute.location = bindingLocation;
1965 }
1966
1967 if (attribute.location != -1)
1968 {
1969 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04001970 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001971
Jamie Madill63805b42015-08-25 13:17:39 -04001972 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001973 {
Jamie Madillf6113162015-05-07 11:49:21 -04001974 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04001975 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001976
1977 return false;
1978 }
1979
Jamie Madill63805b42015-08-25 13:17:39 -04001980 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001981 {
Jamie Madill63805b42015-08-25 13:17:39 -04001982 const int regLocation = attribute.location + reg;
1983 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001984
1985 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04001986 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04001987 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001988 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001989 // TODO(jmadill): fix aliasing on ES2
1990 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001991 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001992 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04001993 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001994 return false;
1995 }
1996 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001997 else
1998 {
Jamie Madill63805b42015-08-25 13:17:39 -04001999 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002000 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002001
Jamie Madill63805b42015-08-25 13:17:39 -04002002 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002003 }
2004 }
2005 }
2006
2007 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002008 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002009 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002010 // Not set by glBindAttribLocation or by location layout qualifier
2011 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002012 {
Jamie Madill63805b42015-08-25 13:17:39 -04002013 int regs = VariableRegisterCount(attribute.type);
2014 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002015
Jamie Madill63805b42015-08-25 13:17:39 -04002016 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002017 {
Jamie Madillf6113162015-05-07 11:49:21 -04002018 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002019 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002020 }
2021
Jamie Madillc349ec02015-08-21 16:53:12 -04002022 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002023 }
2024 }
2025
Jamie Madill48ef11b2016-04-27 15:21:52 -04002026 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002027 {
Jamie Madill63805b42015-08-25 13:17:39 -04002028 ASSERT(attribute.location != -1);
2029 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002030
Jamie Madill63805b42015-08-25 13:17:39 -04002031 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002032 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002033 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002034 }
2035 }
2036
Geoff Lang7dd2e102014-11-10 15:19:26 -05002037 return true;
2038}
2039
Martin Radev4c4c8e72016-08-04 12:25:34 +03002040bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2041 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2042 const std::string &errorMessage,
2043 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002044{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002045 GLuint blockCount = 0;
2046 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002047 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002048 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002049 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002050 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002051 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002052 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002053 return false;
2054 }
2055 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002056 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002057 return true;
2058}
Jamie Madille473dee2015-08-18 14:49:01 -04002059
Martin Radev4c4c8e72016-08-04 12:25:34 +03002060bool Program::validateVertexAndFragmentInterfaceBlocks(
2061 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2062 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2063 InfoLog &infoLog) const
2064{
2065 // Check that interface blocks defined in the vertex and fragment shaders are identical
2066 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2067 UniformBlockMap linkedUniformBlocks;
2068
2069 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2070 {
2071 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2072 }
2073
Jamie Madille473dee2015-08-18 14:49:01 -04002074 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002075 {
Jamie Madille473dee2015-08-18 14:49:01 -04002076 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002077 if (entry != linkedUniformBlocks.end())
2078 {
2079 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2080 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2081 {
2082 return false;
2083 }
2084 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002085 }
2086 return true;
2087}
Jamie Madille473dee2015-08-18 14:49:01 -04002088
Martin Radev4c4c8e72016-08-04 12:25:34 +03002089bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2090{
2091 if (mState.mAttachedComputeShader)
2092 {
2093 const Shader &computeShader = *mState.mAttachedComputeShader;
2094 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2095
2096 if (!validateUniformBlocksCount(
2097 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2098 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2099 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002100 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002101 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002102 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002103 return true;
2104 }
2105
2106 const Shader &vertexShader = *mState.mAttachedVertexShader;
2107 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2108
2109 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2110 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2111
2112 if (!validateUniformBlocksCount(
2113 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2114 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2115 {
2116 return false;
2117 }
2118 if (!validateUniformBlocksCount(
2119 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2120 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2121 infoLog))
2122 {
2123
2124 return false;
2125 }
2126 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2127 infoLog))
2128 {
2129 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130 }
Jamie Madille473dee2015-08-18 14:49:01 -04002131
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132 return true;
2133}
2134
Jamie Madilla2c74982016-12-12 11:20:42 -05002135bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002136 const sh::InterfaceBlock &vertexInterfaceBlock,
2137 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002138{
2139 const char* blockName = vertexInterfaceBlock.name.c_str();
2140 // validate blocks for the same member types
2141 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2142 {
Jamie Madillf6113162015-05-07 11:49:21 -04002143 infoLog << "Types for interface block '" << blockName
2144 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002145 return false;
2146 }
2147 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2148 {
Jamie Madillf6113162015-05-07 11:49:21 -04002149 infoLog << "Array sizes differ for interface block '" << blockName
2150 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002151 return false;
2152 }
2153 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2154 {
Jamie Madillf6113162015-05-07 11:49:21 -04002155 infoLog << "Layout qualifiers differ for interface block '" << blockName
2156 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002157 return false;
2158 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002159 const unsigned int numBlockMembers =
2160 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002161 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2162 {
2163 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2164 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2165 if (vertexMember.name != fragmentMember.name)
2166 {
Jamie Madillf6113162015-05-07 11:49:21 -04002167 infoLog << "Name mismatch for field " << blockMemberIndex
2168 << " of interface block '" << blockName
2169 << "': (in vertex: '" << vertexMember.name
2170 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002171 return false;
2172 }
2173 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2174 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2175 {
2176 return false;
2177 }
2178 }
2179 return true;
2180}
2181
2182bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2183 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2184{
2185 if (vertexVariable.type != fragmentVariable.type)
2186 {
Jamie Madillf6113162015-05-07 11:49:21 -04002187 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002188 return false;
2189 }
2190 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2191 {
Jamie Madillf6113162015-05-07 11:49:21 -04002192 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002193 return false;
2194 }
2195 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2196 {
Jamie Madillf6113162015-05-07 11:49:21 -04002197 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002198 return false;
2199 }
2200
2201 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2202 {
Jamie Madillf6113162015-05-07 11:49:21 -04002203 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002204 return false;
2205 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002206 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002207 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2208 {
2209 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2210 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2211
2212 if (vertexMember.name != fragmentMember.name)
2213 {
Jamie Madillf6113162015-05-07 11:49:21 -04002214 infoLog << "Name mismatch for field '" << memberIndex
2215 << "' of " << variableName
2216 << ": (in vertex: '" << vertexMember.name
2217 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002218 return false;
2219 }
2220
2221 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2222 vertexMember.name + "'";
2223
2224 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2225 {
2226 return false;
2227 }
2228 }
2229
2230 return true;
2231}
2232
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002233bool Program::linkValidateVaryings(InfoLog &infoLog,
2234 const std::string &varyingName,
2235 const sh::Varying &vertexVarying,
2236 const sh::Varying &fragmentVarying,
2237 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002238{
2239 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2240 {
2241 return false;
2242 }
2243
Jamie Madille9cc4692015-02-19 16:00:13 -05002244 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002245 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002246 infoLog << "Interpolation types for " << varyingName
2247 << " differ between vertex and fragment shaders.";
2248 return false;
2249 }
2250
2251 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2252 {
2253 infoLog << "Invariance for " << varyingName
2254 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002255 return false;
2256 }
2257
2258 return true;
2259}
2260
Yuly Novikov817232e2017-02-22 18:36:10 -05002261bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
2262{
2263 const Shader *vertexShader = mState.mAttachedVertexShader;
2264 const Shader *fragmentShader = mState.mAttachedFragmentShader;
2265 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
2266 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
2267 int shaderVersion = vertexShader->getShaderVersion();
2268
2269 if (shaderVersion != 100)
2270 {
2271 // Only ESSL 1.0 has restrictions on matching input and output invariance
2272 return true;
2273 }
2274
2275 bool glPositionIsInvariant = false;
2276 bool glPointSizeIsInvariant = false;
2277 bool glFragCoordIsInvariant = false;
2278 bool glPointCoordIsInvariant = false;
2279
2280 for (const sh::Varying &varying : vertexVaryings)
2281 {
2282 if (!varying.isBuiltIn())
2283 {
2284 continue;
2285 }
2286 if (varying.name.compare("gl_Position") == 0)
2287 {
2288 glPositionIsInvariant = varying.isInvariant;
2289 }
2290 else if (varying.name.compare("gl_PointSize") == 0)
2291 {
2292 glPointSizeIsInvariant = varying.isInvariant;
2293 }
2294 }
2295
2296 for (const sh::Varying &varying : fragmentVaryings)
2297 {
2298 if (!varying.isBuiltIn())
2299 {
2300 continue;
2301 }
2302 if (varying.name.compare("gl_FragCoord") == 0)
2303 {
2304 glFragCoordIsInvariant = varying.isInvariant;
2305 }
2306 else if (varying.name.compare("gl_PointCoord") == 0)
2307 {
2308 glPointCoordIsInvariant = varying.isInvariant;
2309 }
2310 }
2311
2312 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2313 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2314 // Not requiring invariance to match is supported by:
2315 // dEQP, WebGL CTS, Nexus 5X GLES
2316 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2317 {
2318 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2319 "declared invariant.";
2320 return false;
2321 }
2322 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2323 {
2324 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2325 "declared invariant.";
2326 return false;
2327 }
2328
2329 return true;
2330}
2331
Jamie Madillccdf74b2015-08-18 10:46:12 -04002332bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002333 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002334 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002335{
2336 size_t totalComponents = 0;
2337
Jamie Madillccdf74b2015-08-18 10:46:12 -04002338 std::set<std::string> uniqueNames;
2339
Jamie Madill48ef11b2016-04-27 15:21:52 -04002340 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002341 {
2342 bool found = false;
Jamie Madill192745a2016-12-22 15:58:21 -05002343 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002344 {
Jamie Madill192745a2016-12-22 15:58:21 -05002345 const sh::Varying *varying = ref.second.get();
2346
Jamie Madillccdf74b2015-08-18 10:46:12 -04002347 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002348 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002349 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002350 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002351 infoLog << "Two transform feedback varyings specify the same output variable ("
2352 << tfVaryingName << ").";
2353 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002354 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002355 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002356
Geoff Lang1a683462015-09-29 15:09:59 -04002357 if (varying->isArray())
2358 {
2359 infoLog << "Capture of arrays is undefined and not supported.";
2360 return false;
2361 }
2362
Jamie Madillccdf74b2015-08-18 10:46:12 -04002363 // TODO(jmadill): Investigate implementation limits on D3D11
Jamie Madilla2c74982016-12-12 11:20:42 -05002364 size_t componentCount = VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002365 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002366 componentCount > caps.maxTransformFeedbackSeparateComponents)
2367 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002368 infoLog << "Transform feedback varying's " << varying->name << " components ("
2369 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002370 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002371 return false;
2372 }
2373
2374 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002375 found = true;
2376 break;
2377 }
2378 }
2379
Jamie Madill89bb70e2015-08-31 14:18:39 -04002380 if (tfVaryingName.find('[') != std::string::npos)
2381 {
Geoff Lang1a683462015-09-29 15:09:59 -04002382 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002383 return false;
2384 }
2385
Geoff Lang7dd2e102014-11-10 15:19:26 -05002386 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2387 ASSERT(found);
2388 }
2389
Jamie Madill48ef11b2016-04-27 15:21:52 -04002390 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002391 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002392 {
Jamie Madillf6113162015-05-07 11:49:21 -04002393 infoLog << "Transform feedback varying total components (" << totalComponents
2394 << ") exceed the maximum interleaved components ("
2395 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002396 return false;
2397 }
2398
2399 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002400}
2401
Jamie Madill192745a2016-12-22 15:58:21 -05002402void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002403{
2404 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002405 mState.mTransformFeedbackVaryingVars.clear();
2406 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002407 {
Jamie Madill192745a2016-12-22 15:58:21 -05002408 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002409 {
Jamie Madill192745a2016-12-22 15:58:21 -05002410 const sh::Varying *varying = ref.second.get();
Jamie Madillccdf74b2015-08-18 10:46:12 -04002411 if (tfVaryingName == varying->name)
2412 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002413 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002414 break;
2415 }
2416 }
2417 }
2418}
2419
Jamie Madill192745a2016-12-22 15:58:21 -05002420Program::MergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002421{
Jamie Madill192745a2016-12-22 15:58:21 -05002422 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002423
Jamie Madill48ef11b2016-04-27 15:21:52 -04002424 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002425 {
Jamie Madill192745a2016-12-22 15:58:21 -05002426 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002427 }
2428
Jamie Madill48ef11b2016-04-27 15:21:52 -04002429 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002430 {
Jamie Madill192745a2016-12-22 15:58:21 -05002431 merged[varying.name].fragment = &varying;
2432 }
2433
2434 return merged;
2435}
2436
2437std::vector<PackedVarying> Program::getPackedVaryings(
2438 const Program::MergedVaryings &mergedVaryings) const
2439{
2440 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2441 std::vector<PackedVarying> packedVaryings;
2442
2443 for (const auto &ref : mergedVaryings)
2444 {
2445 const sh::Varying *input = ref.second.vertex;
2446 const sh::Varying *output = ref.second.fragment;
2447
2448 // Only pack varyings that have a matched input or output, plus special builtins.
2449 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002450 {
Jamie Madill192745a2016-12-22 15:58:21 -05002451 // Will get the vertex shader interpolation by default.
2452 auto interpolation = ref.second.get()->interpolation;
2453
2454 // Interpolation qualifiers must match.
2455 if (output->isStruct())
2456 {
2457 ASSERT(!output->isArray());
2458 for (const auto &field : output->fields)
2459 {
2460 ASSERT(!field.isStruct() && !field.isArray());
2461 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2462 }
2463 }
2464 else
2465 {
2466 packedVaryings.push_back(PackedVarying(*output, interpolation));
2467 }
2468 continue;
2469 }
2470
2471 // Keep Transform FB varyings in the merged list always.
2472 if (!input)
2473 {
2474 continue;
2475 }
2476
2477 for (const std::string &tfVarying : tfVaryings)
2478 {
2479 if (tfVarying == input->name)
2480 {
2481 // Transform feedback for varying structs is underspecified.
2482 // See Khronos bug 9856.
2483 // TODO(jmadill): Figure out how to be spec-compliant here.
2484 if (!input->isStruct())
2485 {
2486 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2487 packedVaryings.back().vertexOnly = true;
2488 }
2489 break;
2490 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002491 }
2492 }
2493
Jamie Madill192745a2016-12-22 15:58:21 -05002494 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2495
2496 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002497}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002498
2499void Program::linkOutputVariables()
2500{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002501 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002502 ASSERT(fragmentShader != nullptr);
2503
2504 // Skip this step for GLES2 shaders.
2505 if (fragmentShader->getShaderVersion() == 100)
2506 return;
2507
Jamie Madilla0a9e122015-09-02 15:54:30 -04002508 const auto &shaderOutputVars = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002509
2510 // TODO(jmadill): any caps validation here?
2511
2512 for (unsigned int outputVariableIndex = 0; outputVariableIndex < shaderOutputVars.size();
2513 outputVariableIndex++)
2514 {
Jamie Madilla0a9e122015-09-02 15:54:30 -04002515 const sh::OutputVariable &outputVariable = shaderOutputVars[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002516
2517 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2518 if (outputVariable.isBuiltIn())
2519 continue;
2520
2521 // Since multiple output locations must be specified, use 0 for non-specified locations.
2522 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2523
Jamie Madill80a6fc02015-08-21 16:53:16 -04002524 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2525 elementIndex++)
2526 {
2527 const int location = baseLocation + elementIndex;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002528 ASSERT(mState.mOutputVariables.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002529 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002530 mState.mOutputVariables[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002531 VariableLocation(outputVariable.name, element, outputVariableIndex);
2532 }
2533 }
2534}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002535
Jamie Madill62d31cb2015-09-11 13:25:51 -04002536void Program::gatherInterfaceBlockInfo()
2537{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002538 ASSERT(mState.mUniformBlocks.empty());
2539
2540 if (mState.mAttachedComputeShader)
2541 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002542 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002543
2544 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2545 {
2546
2547 // Only 'packed' blocks are allowed to be considered inactive.
2548 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2549 continue;
2550
Jamie Madilla2c74982016-12-12 11:20:42 -05002551 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002552 {
2553 if (block.name == computeBlock.name)
2554 {
2555 block.computeStaticUse = computeBlock.staticUse;
2556 }
2557 }
2558
2559 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2560 }
2561 return;
2562 }
2563
Jamie Madill62d31cb2015-09-11 13:25:51 -04002564 std::set<std::string> visitedList;
2565
Jamie Madilla2c74982016-12-12 11:20:42 -05002566 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002567
Jamie Madill62d31cb2015-09-11 13:25:51 -04002568 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2569 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002570 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002571 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2572 continue;
2573
2574 if (visitedList.count(vertexBlock.name) > 0)
2575 continue;
2576
2577 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2578 visitedList.insert(vertexBlock.name);
2579 }
2580
Jamie Madilla2c74982016-12-12 11:20:42 -05002581 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002582
2583 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2584 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002585 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002586 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2587 continue;
2588
2589 if (visitedList.count(fragmentBlock.name) > 0)
2590 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002591 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002592 {
2593 if (block.name == fragmentBlock.name)
2594 {
2595 block.fragmentStaticUse = fragmentBlock.staticUse;
2596 }
2597 }
2598
2599 continue;
2600 }
2601
2602 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2603 visitedList.insert(fragmentBlock.name);
2604 }
2605}
2606
Jamie Madill4a3c2342015-10-08 12:58:45 -04002607template <typename VarT>
2608void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2609 const std::string &prefix,
2610 int blockIndex)
2611{
2612 for (const VarT &field : fields)
2613 {
2614 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2615
2616 if (field.isStruct())
2617 {
2618 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2619 {
2620 const std::string uniformElementName =
2621 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2622 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2623 }
2624 }
2625 else
2626 {
2627 // If getBlockMemberInfo returns false, the uniform is optimized out.
2628 sh::BlockMemberInfo memberInfo;
2629 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2630 {
2631 continue;
2632 }
2633
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002634 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002635 blockIndex, memberInfo);
2636
2637 // Since block uniforms have no location, we don't need to store them in the uniform
2638 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002639 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002640 }
2641 }
2642}
2643
Jamie Madill62d31cb2015-09-11 13:25:51 -04002644void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2645{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002646 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002647 size_t blockSize = 0;
2648
2649 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002650 std::stringstream blockNameStr;
2651 blockNameStr << interfaceBlock.name;
2652 if (interfaceBlock.arraySize > 0)
2653 {
2654 blockNameStr << "[0]";
2655 }
2656 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002657 {
2658 return;
2659 }
2660
2661 // Track the first and last uniform index to determine the range of active uniforms in the
2662 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002663 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002664 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002665 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002666
2667 std::vector<unsigned int> blockUniformIndexes;
2668 for (size_t blockUniformIndex = firstBlockUniformIndex;
2669 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2670 {
2671 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2672 }
2673
2674 if (interfaceBlock.arraySize > 0)
2675 {
2676 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2677 {
2678 UniformBlock block(interfaceBlock.name, true, arrayElement);
2679 block.memberUniformIndexes = blockUniformIndexes;
2680
Martin Radev4c4c8e72016-08-04 12:25:34 +03002681 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002682 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002683 case GL_VERTEX_SHADER:
2684 {
2685 block.vertexStaticUse = interfaceBlock.staticUse;
2686 break;
2687 }
2688 case GL_FRAGMENT_SHADER:
2689 {
2690 block.fragmentStaticUse = interfaceBlock.staticUse;
2691 break;
2692 }
2693 case GL_COMPUTE_SHADER:
2694 {
2695 block.computeStaticUse = interfaceBlock.staticUse;
2696 break;
2697 }
2698 default:
2699 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002700 }
2701
Qin Jiajia0350a642016-11-01 17:01:51 +08002702 // Since all block elements in an array share the same active uniforms, they will all be
2703 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2704 // here we will add every block element in the array.
2705 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002706 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002707 }
2708 }
2709 else
2710 {
2711 UniformBlock block(interfaceBlock.name, false, 0);
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
Jamie Madill4a3c2342015-10-08 12:58:45 -04002735 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002736 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002737 }
2738}
2739
Jamie Madille7d84322017-01-10 18:21:59 -05002740template <>
2741void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2742 const uint8_t *destPointer,
2743 GLsizei clampedCount,
2744 const GLint *v)
2745{
2746 // Invalidate the validation cache only if we modify the sampler data.
2747 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2748 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2749 {
2750 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2751 std::vector<GLuint> *boundTextureUnits =
2752 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2753
2754 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2755 mCachedValidateSamplersResult.reset();
2756 }
2757}
2758
2759template <typename T>
2760void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2761 const uint8_t *destPointer,
2762 GLsizei clampedCount,
2763 const T *v)
2764{
2765}
2766
Jamie Madill62d31cb2015-09-11 13:25:51 -04002767template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002768GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002769{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002770 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2771 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002772 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2773
Corentin Wallez15ac5342016-11-03 17:06:39 -04002774 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2775 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2776 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002777 GLsizei maxElementCount =
2778 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2779
2780 GLsizei count = countIn;
2781 GLsizei clampedCount = count * vectorSize;
2782 if (clampedCount > maxElementCount)
2783 {
2784 clampedCount = maxElementCount;
2785 count = maxElementCount / vectorSize;
2786 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002787
Jamie Madill62d31cb2015-09-11 13:25:51 -04002788 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2789 {
2790 // Do a cast conversion for boolean types. From the spec:
2791 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2792 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002793 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002794 {
2795 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2796 }
2797 }
2798 else
2799 {
Jamie Madille7d84322017-01-10 18:21:59 -05002800 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002801 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002802 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002803
2804 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002805}
2806
2807template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002808GLsizei Program::setMatrixUniformInternal(GLint location,
2809 GLsizei count,
2810 GLboolean transpose,
2811 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002812{
2813 if (!transpose)
2814 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002815 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002816 }
2817
2818 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002819 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2820 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002821 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002822
2823 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2824 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2825 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2826 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2827
2828 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002829 {
2830 size_t elementOffset = element * rows * cols;
2831
2832 for (size_t row = 0; row < rows; ++row)
2833 {
2834 for (size_t col = 0; col < cols; ++col)
2835 {
2836 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2837 }
2838 }
2839 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002840
2841 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002842}
2843
2844template <typename DestT>
2845void Program::getUniformInternal(GLint location, DestT *dataOut) const
2846{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002847 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2848 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002849
2850 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2851
2852 GLenum componentType = VariableComponentType(uniform.type);
2853 if (componentType == GLTypeToGLenum<DestT>::value)
2854 {
2855 memcpy(dataOut, srcPointer, uniform.getElementSize());
2856 return;
2857 }
2858
Corentin Wallez6596c462016-03-17 17:26:58 -04002859 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002860
2861 switch (componentType)
2862 {
2863 case GL_INT:
2864 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2865 break;
2866 case GL_UNSIGNED_INT:
2867 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2868 break;
2869 case GL_BOOL:
2870 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2871 break;
2872 case GL_FLOAT:
2873 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2874 break;
2875 default:
2876 UNREACHABLE();
2877 }
2878}
Jamie Madilla4595b82017-01-11 17:36:34 -05002879
2880bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
2881{
2882 // Must be called after samplers are validated.
2883 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
2884
2885 for (const auto &binding : mState.mSamplerBindings)
2886 {
2887 GLenum textureType = binding.textureType;
2888 for (const auto &unit : binding.boundTextureUnits)
2889 {
2890 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
2891 if (programTextureID == textureID)
2892 {
2893 // TODO(jmadill): Check for appropriate overlap.
2894 return true;
2895 }
2896 }
2897 }
2898
2899 return false;
2900}
2901
Jamie Madilla2c74982016-12-12 11:20:42 -05002902} // namespace gl