blob: ebedc4b34e015d18fae4ad403cd7918113451234 [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 Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.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
jchen1015015f72017-03-16 13:54:21 +0800135template <typename VarT>
136GLuint GetResourceIndexFromName(const std::vector<VarT> &list, const std::string &name)
137{
138 size_t subscript = GL_INVALID_INDEX;
139 std::string baseName = ParseResourceName(name, &subscript);
140
141 // The app is not allowed to specify array indices other than 0 for arrays of basic types
142 if (subscript != 0 && subscript != GL_INVALID_INDEX)
143 {
144 return GL_INVALID_INDEX;
145 }
146
147 for (size_t index = 0; index < list.size(); index++)
148 {
149 const VarT &resource = list[index];
150 if (resource.name == baseName)
151 {
152 if (resource.isArray() || subscript == GL_INVALID_INDEX)
153 {
154 return static_cast<GLuint>(index);
155 }
156 }
157 }
158
159 return GL_INVALID_INDEX;
160}
161
jchen10fd7c3b52017-03-21 15:36:03 +0800162void CopyStringToBuffer(GLchar *buffer, const std::string &string, GLsizei bufSize, GLsizei *length)
163{
164 ASSERT(bufSize > 0);
165 strncpy(buffer, string.c_str(), bufSize);
166 buffer[bufSize - 1] = '\0';
167
168 if (length)
169 {
170 *length = static_cast<GLsizei>(strlen(buffer));
171 }
172}
173
Jamie Madill62d31cb2015-09-11 13:25:51 -0400174} // anonymous namespace
175
Jamie Madill4a3c2342015-10-08 12:58:45 -0400176const char *const g_fakepath = "C:\\fakepath";
177
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400178InfoLog::InfoLog()
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000179{
180}
181
182InfoLog::~InfoLog()
183{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000184}
185
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400186size_t InfoLog::getLength() const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000187{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400188 const std::string &logString = mStream.str();
189 return logString.empty() ? 0 : logString.length() + 1;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000190}
191
Geoff Lange1a27752015-10-05 13:16:04 -0400192void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000193{
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400194 size_t index = 0;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000195
196 if (bufSize > 0)
197 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400198 const std::string str(mStream.str());
199
200 if (!str.empty())
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000201 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400202 index = std::min(static_cast<size_t>(bufSize) - 1, str.length());
203 memcpy(infoLog, str.c_str(), index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000204 }
205
206 infoLog[index] = '\0';
207 }
208
209 if (length)
210 {
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400211 *length = static_cast<GLsizei>(index);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000212 }
213}
214
215// append a santized message to the program info log.
Sami Väisänen46eaa942016-06-29 10:26:37 +0300216// The D3D compiler includes a fake file path in some of the warning or error
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000217// messages, so lets remove all occurrences of this fake file path from the log.
218void InfoLog::appendSanitized(const char *message)
219{
220 std::string msg(message);
221
222 size_t found;
223 do
224 {
225 found = msg.find(g_fakepath);
226 if (found != std::string::npos)
227 {
228 msg.erase(found, strlen(g_fakepath));
229 }
230 }
231 while (found != std::string::npos);
232
Jamie Madill71c3b2c2015-05-07 11:49:20 -0400233 mStream << message << std::endl;
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000234}
235
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000236void InfoLog::reset()
237{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000238}
239
Geoff Langd8605522016-04-13 10:19:12 -0400240VariableLocation::VariableLocation() : name(), element(0), index(0), used(false), ignored(false)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000241{
Geoff Lang7dd2e102014-11-10 15:19:26 -0500242}
243
Geoff Langd8605522016-04-13 10:19:12 -0400244VariableLocation::VariableLocation(const std::string &name,
245 unsigned int element,
246 unsigned int index)
247 : name(name), element(element), index(index), used(true), ignored(false)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500248{
249}
250
Geoff Langd8605522016-04-13 10:19:12 -0400251void Program::Bindings::bindLocation(GLuint index, const std::string &name)
252{
253 mBindings[name] = index;
254}
255
256int Program::Bindings::getBinding(const std::string &name) const
257{
258 auto iter = mBindings.find(name);
259 return (iter != mBindings.end()) ? iter->second : -1;
260}
261
262Program::Bindings::const_iterator Program::Bindings::begin() const
263{
264 return mBindings.begin();
265}
266
267Program::Bindings::const_iterator Program::Bindings::end() const
268{
269 return mBindings.end();
270}
271
Jamie Madill48ef11b2016-04-27 15:21:52 -0400272ProgramState::ProgramState()
Geoff Lang70d0f492015-12-10 17:45:46 -0500273 : mLabel(),
274 mAttachedFragmentShader(nullptr),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400275 mAttachedVertexShader(nullptr),
Martin Radev4c4c8e72016-08-04 12:25:34 +0300276 mAttachedComputeShader(nullptr),
Geoff Langc5629752015-12-07 16:29:04 -0500277 mTransformFeedbackBufferMode(GL_INTERLEAVED_ATTRIBS),
Jamie Madille7d84322017-01-10 18:21:59 -0500278 mSamplerUniformRange(0, 0),
Geoff Langc5629752015-12-07 16:29:04 -0500279 mBinaryRetrieveableHint(false)
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400280{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300281 mComputeShaderLocalSize.fill(1);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400282}
283
Jamie Madill48ef11b2016-04-27 15:21:52 -0400284ProgramState::~ProgramState()
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400285{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500286 ASSERT(!mAttachedVertexShader && !mAttachedFragmentShader && !mAttachedComputeShader);
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400287}
288
Jamie Madill48ef11b2016-04-27 15:21:52 -0400289const std::string &ProgramState::getLabel()
Geoff Lang70d0f492015-12-10 17:45:46 -0500290{
291 return mLabel;
292}
293
Jamie Madill48ef11b2016-04-27 15:21:52 -0400294GLint ProgramState::getUniformLocation(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400295{
296 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +0800297 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400298
299 for (size_t location = 0; location < mUniformLocations.size(); ++location)
300 {
301 const VariableLocation &uniformLocation = mUniformLocations[location];
Geoff Langd8605522016-04-13 10:19:12 -0400302 if (!uniformLocation.used)
303 {
304 continue;
305 }
306
307 const LinkedUniform &uniform = mUniforms[uniformLocation.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -0400308
309 if (uniform.name == baseName)
310 {
Geoff Langd8605522016-04-13 10:19:12 -0400311 if (uniform.isArray())
Jamie Madill62d31cb2015-09-11 13:25:51 -0400312 {
Geoff Langd8605522016-04-13 10:19:12 -0400313 if (uniformLocation.element == subscript ||
314 (uniformLocation.element == 0 && subscript == GL_INVALID_INDEX))
315 {
316 return static_cast<GLint>(location);
317 }
318 }
319 else
320 {
321 if (subscript == GL_INVALID_INDEX)
322 {
323 return static_cast<GLint>(location);
324 }
Jamie Madill62d31cb2015-09-11 13:25:51 -0400325 }
326 }
327 }
328
329 return -1;
330}
331
Jamie Madille7d84322017-01-10 18:21:59 -0500332GLuint ProgramState::getUniformIndexFromName(const std::string &name) const
Jamie Madill62d31cb2015-09-11 13:25:51 -0400333{
jchen1015015f72017-03-16 13:54:21 +0800334 return GetResourceIndexFromName(mUniforms, name);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400335}
336
Jamie Madille7d84322017-01-10 18:21:59 -0500337GLuint ProgramState::getUniformIndexFromLocation(GLint location) const
338{
339 ASSERT(location >= 0 && static_cast<size_t>(location) < mUniformLocations.size());
340 return mUniformLocations[location].index;
341}
342
343Optional<GLuint> ProgramState::getSamplerIndex(GLint location) const
344{
345 GLuint index = getUniformIndexFromLocation(location);
346 if (!isSamplerUniformIndex(index))
347 {
348 return Optional<GLuint>::Invalid();
349 }
350
351 return getSamplerIndexFromUniformIndex(index);
352}
353
354bool ProgramState::isSamplerUniformIndex(GLuint index) const
355{
356 return index >= mSamplerUniformRange.start && index < mSamplerUniformRange.end;
357}
358
359GLuint ProgramState::getSamplerIndexFromUniformIndex(GLuint uniformIndex) const
360{
361 ASSERT(isSamplerUniformIndex(uniformIndex));
362 return uniformIndex - mSamplerUniformRange.start;
363}
364
Geoff Lang4ddf5af2016-12-01 14:30:44 -0500365Program::Program(rx::GLImplFactory *factory, ShaderProgramManager *manager, GLuint handle)
Jamie Madill48ef11b2016-04-27 15:21:52 -0400366 : mProgram(factory->createProgram(mState)),
Jamie Madill5c6b7bf2015-08-17 12:53:35 -0400367 mValidated(false),
Geoff Lang7dd2e102014-11-10 15:19:26 -0500368 mLinked(false),
369 mDeleteStatus(false),
370 mRefCount(0),
371 mResourceManager(manager),
Jamie Madille7d84322017-01-10 18:21:59 -0500372 mHandle(handle)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500373{
374 ASSERT(mProgram);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000375
376 resetUniformBlockBindings();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500377 unlink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000378}
379
380Program::~Program()
381{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500382 ASSERT(!mState.mAttachedVertexShader && !mState.mAttachedFragmentShader &&
383 !mState.mAttachedComputeShader);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500384 SafeDelete(mProgram);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385}
386
Jamie Madill6c1f6712017-02-14 19:08:04 -0500387void Program::destroy(const Context *context)
388{
389 if (mState.mAttachedVertexShader != nullptr)
390 {
391 mState.mAttachedVertexShader->release(context);
392 mState.mAttachedVertexShader = nullptr;
393 }
394
395 if (mState.mAttachedFragmentShader != nullptr)
396 {
397 mState.mAttachedFragmentShader->release(context);
398 mState.mAttachedFragmentShader = nullptr;
399 }
400
401 if (mState.mAttachedComputeShader != nullptr)
402 {
403 mState.mAttachedComputeShader->release(context);
404 mState.mAttachedComputeShader = nullptr;
405 }
406
407 mProgram->destroy(rx::SafeGetImpl(context));
408}
409
Geoff Lang70d0f492015-12-10 17:45:46 -0500410void Program::setLabel(const std::string &label)
411{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400412 mState.mLabel = label;
Geoff Lang70d0f492015-12-10 17:45:46 -0500413}
414
415const std::string &Program::getLabel() const
416{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400417 return mState.mLabel;
Geoff Lang70d0f492015-12-10 17:45:46 -0500418}
419
Jamie Madillef300b12016-10-07 15:12:09 -0400420void Program::attachShader(Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300422 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300424 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425 {
Jamie Madillef300b12016-10-07 15:12:09 -0400426 ASSERT(!mState.mAttachedVertexShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300427 mState.mAttachedVertexShader = shader;
428 mState.mAttachedVertexShader->addRef();
429 break;
430 }
431 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432 {
Jamie Madillef300b12016-10-07 15:12:09 -0400433 ASSERT(!mState.mAttachedFragmentShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300434 mState.mAttachedFragmentShader = shader;
435 mState.mAttachedFragmentShader->addRef();
436 break;
437 }
438 case GL_COMPUTE_SHADER:
439 {
Jamie Madillef300b12016-10-07 15:12:09 -0400440 ASSERT(!mState.mAttachedComputeShader);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300441 mState.mAttachedComputeShader = shader;
442 mState.mAttachedComputeShader->addRef();
443 break;
444 }
445 default:
446 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000447 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448}
449
Jamie Madill6c1f6712017-02-14 19:08:04 -0500450bool Program::detachShader(const Context *context, Shader *shader)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000451{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300452 switch (shader->getType())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300454 case GL_VERTEX_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300456 if (mState.mAttachedVertexShader != shader)
457 {
458 return false;
459 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460
Jamie Madill6c1f6712017-02-14 19:08:04 -0500461 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300462 mState.mAttachedVertexShader = nullptr;
463 break;
464 }
465 case GL_FRAGMENT_SHADER:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300467 if (mState.mAttachedFragmentShader != shader)
468 {
469 return false;
470 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471
Jamie Madill6c1f6712017-02-14 19:08:04 -0500472 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300473 mState.mAttachedFragmentShader = nullptr;
474 break;
475 }
476 case GL_COMPUTE_SHADER:
477 {
478 if (mState.mAttachedComputeShader != shader)
479 {
480 return false;
481 }
482
Jamie Madill6c1f6712017-02-14 19:08:04 -0500483 shader->release(context);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300484 mState.mAttachedComputeShader = nullptr;
485 break;
486 }
487 default:
488 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491 return true;
492}
493
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000494int Program::getAttachedShadersCount() const
495{
Martin Radev4c4c8e72016-08-04 12:25:34 +0300496 return (mState.mAttachedVertexShader ? 1 : 0) + (mState.mAttachedFragmentShader ? 1 : 0) +
497 (mState.mAttachedComputeShader ? 1 : 0);
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000498}
499
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500void Program::bindAttributeLocation(GLuint index, const char *name)
501{
Geoff Langd8605522016-04-13 10:19:12 -0400502 mAttributeBindings.bindLocation(index, name);
503}
504
505void Program::bindUniformLocation(GLuint index, const char *name)
506{
507 // Bind the base uniform name only since array indices other than 0 cannot be bound
jchen1015015f72017-03-16 13:54:21 +0800508 mUniformLocationBindings.bindLocation(index, ParseResourceName(name, nullptr));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509}
510
Sami Väisänen46eaa942016-06-29 10:26:37 +0300511void Program::bindFragmentInputLocation(GLint index, const char *name)
512{
513 mFragmentInputBindings.bindLocation(index, name);
514}
515
516BindingInfo Program::getFragmentInputBindingInfo(GLint index) const
517{
518 BindingInfo ret;
519 ret.type = GL_NONE;
520 ret.valid = false;
521
522 const Shader *fragmentShader = mState.getAttachedFragmentShader();
523 ASSERT(fragmentShader);
524
525 // Find the actual fragment shader varying we're interested in
526 const std::vector<sh::Varying> &inputs = fragmentShader->getVaryings();
527
528 for (const auto &binding : mFragmentInputBindings)
529 {
530 if (binding.second != static_cast<GLuint>(index))
531 continue;
532
533 ret.valid = true;
534
535 std::string originalName = binding.first;
Geoff Lang3f6a3982016-07-15 15:20:45 -0400536 unsigned int arrayIndex = ParseAndStripArrayIndex(&originalName);
Sami Väisänen46eaa942016-06-29 10:26:37 +0300537
538 for (const auto &in : inputs)
539 {
540 if (in.name == originalName)
541 {
542 if (in.isArray())
543 {
544 // The client wants to bind either "name" or "name[0]".
545 // GL ES 3.1 spec refers to active array names with language such as:
546 // "if the string identifies the base name of an active array, where the
547 // string would exactly match the name of the variable if the suffix "[0]"
548 // were appended to the string".
Geoff Lang3f6a3982016-07-15 15:20:45 -0400549 if (arrayIndex == GL_INVALID_INDEX)
550 arrayIndex = 0;
Sami Väisänen46eaa942016-06-29 10:26:37 +0300551
Corentin Wallez054f7ed2016-09-20 17:15:59 -0400552 ret.name = in.mappedName + "[" + ToString(arrayIndex) + "]";
Sami Väisänen46eaa942016-06-29 10:26:37 +0300553 }
554 else
555 {
556 ret.name = in.mappedName;
557 }
558 ret.type = in.type;
559 return ret;
560 }
561 }
562 }
563
564 return ret;
565}
566
567void Program::pathFragmentInputGen(GLint index,
568 GLenum genMode,
569 GLint components,
570 const GLfloat *coeffs)
571{
572 // If the location is -1 then the command is silently ignored
573 if (index == -1)
574 return;
575
576 const auto &binding = getFragmentInputBindingInfo(index);
577
578 // If the input doesn't exist then then the command is silently ignored
579 // This could happen through optimization for example, the shader translator
580 // decides that a variable is not actually being used and optimizes it away.
581 if (binding.name.empty())
582 return;
583
584 mProgram->setPathFragmentInputGen(binding.name, genMode, components, coeffs);
585}
586
Martin Radev4c4c8e72016-08-04 12:25:34 +0300587// The attached shaders are checked for linking errors by matching up their variables.
588// Uniform, input and output variables get collected.
589// The code gets compiled into binaries.
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500590Error Program::link(const gl::Context *context)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000591{
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500592 const auto &data = context->getContextState();
593
Jamie Madill6c1f6712017-02-14 19:08:04 -0500594 unlink();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +0000595
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000596 mInfoLog.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000597 resetUniformBlockBindings();
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000598
Martin Radev4c4c8e72016-08-04 12:25:34 +0300599 const Caps &caps = data.getCaps();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500600
Jamie Madill192745a2016-12-22 15:58:21 -0500601 auto vertexShader = mState.mAttachedVertexShader;
602 auto fragmentShader = mState.mAttachedFragmentShader;
603 auto computeShader = mState.mAttachedComputeShader;
604
605 bool isComputeShaderAttached = (computeShader != nullptr);
606 bool nonComputeShadersAttached = (vertexShader != nullptr || fragmentShader != nullptr);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300607 // Check whether we both have a compute and non-compute shaders attached.
608 // If there are of both types attached, then linking should fail.
609 // OpenGL ES 3.10, 7.3 Program Objects, under LinkProgram
610 if (isComputeShaderAttached == true && nonComputeShadersAttached == true)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500611 {
Martin Radev4c4c8e72016-08-04 12:25:34 +0300612 mInfoLog << "Both a compute and non-compute shaders are attached to the same program.";
613 return NoError();
Yuly Novikovcfa48d32016-06-15 22:14:36 -0400614 }
615
Jamie Madill192745a2016-12-22 15:58:21 -0500616 if (computeShader)
Jamie Madill437d2662014-12-05 14:23:35 -0500617 {
Jamie Madill192745a2016-12-22 15:58:21 -0500618 if (!computeShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300619 {
620 mInfoLog << "Attached compute shader is not compiled.";
621 return NoError();
622 }
Jamie Madill192745a2016-12-22 15:58:21 -0500623 ASSERT(computeShader->getType() == GL_COMPUTE_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300624
Jamie Madill192745a2016-12-22 15:58:21 -0500625 mState.mComputeShaderLocalSize = computeShader->getWorkGroupSize();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300626
627 // GLSL ES 3.10, 4.4.1.1 Compute Shader Inputs
628 // If the work group size is not specified, a link time error should occur.
629 if (!mState.mComputeShaderLocalSize.isDeclared())
630 {
631 mInfoLog << "Work group size is not specified.";
632 return NoError();
633 }
634
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000635 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300636 {
637 return NoError();
638 }
639
640 if (!linkUniformBlocks(mInfoLog, caps))
641 {
642 return NoError();
643 }
644
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500645 gl::VaryingPacking noPacking(0, PackMode::ANGLE_RELAXED);
646 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), noPacking, mInfoLog),
647 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500648 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300649 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500650 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300651 }
652 }
653 else
654 {
Jamie Madill192745a2016-12-22 15:58:21 -0500655 if (!fragmentShader || !fragmentShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300656 {
657 return NoError();
658 }
Jamie Madill192745a2016-12-22 15:58:21 -0500659 ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300660
Jamie Madill192745a2016-12-22 15:58:21 -0500661 if (!vertexShader || !vertexShader->isCompiled())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300662 {
663 return NoError();
664 }
Jamie Madill192745a2016-12-22 15:58:21 -0500665 ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
Martin Radev4c4c8e72016-08-04 12:25:34 +0300666
Jamie Madill192745a2016-12-22 15:58:21 -0500667 if (fragmentShader->getShaderVersion() != vertexShader->getShaderVersion())
Martin Radev4c4c8e72016-08-04 12:25:34 +0300668 {
669 mInfoLog << "Fragment shader version does not match vertex shader version.";
670 return NoError();
671 }
672
Jamie Madilleb979bf2016-11-15 12:28:46 -0500673 if (!linkAttributes(data, mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300674 {
675 return NoError();
676 }
677
Jamie Madill192745a2016-12-22 15:58:21 -0500678 if (!linkVaryings(mInfoLog))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300679 {
680 return NoError();
681 }
682
Olli Etuaho4a92ceb2017-02-19 17:51:24 +0000683 if (!linkUniforms(mInfoLog, caps, mUniformLocationBindings))
Martin Radev4c4c8e72016-08-04 12:25:34 +0300684 {
685 return NoError();
686 }
687
688 if (!linkUniformBlocks(mInfoLog, caps))
689 {
690 return NoError();
691 }
692
693 const auto &mergedVaryings = getMergedVaryings();
694
695 if (!linkValidateTransformFeedback(mInfoLog, mergedVaryings, caps))
696 {
697 return NoError();
698 }
699
700 linkOutputVariables();
701
Jamie Madill192745a2016-12-22 15:58:21 -0500702 // Validate we can pack the varyings.
703 std::vector<PackedVarying> packedVaryings = getPackedVaryings(mergedVaryings);
704
705 // Map the varyings to the register file
706 // In WebGL, we use a slightly different handling for packing variables.
707 auto packMode = data.getExtensions().webglCompatibility ? PackMode::WEBGL_STRICT
708 : PackMode::ANGLE_RELAXED;
709 VaryingPacking varyingPacking(data.getCaps().maxVaryingVectors, packMode);
710 if (!varyingPacking.packUserVaryings(mInfoLog, packedVaryings,
711 mState.getTransformFeedbackVaryingNames()))
712 {
713 return NoError();
714 }
715
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500716 ANGLE_TRY_RESULT(mProgram->link(context->getImplementation(), varyingPacking, mInfoLog),
717 mLinked);
Jamie Madillb0a838b2016-11-13 20:02:12 -0500718 if (!mLinked)
Martin Radev4c4c8e72016-08-04 12:25:34 +0300719 {
Jamie Madillb0a838b2016-11-13 20:02:12 -0500720 return NoError();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300721 }
722
723 gatherTransformFeedbackVaryings(mergedVaryings);
Jamie Madill437d2662014-12-05 14:23:35 -0500724 }
725
Olli Etuaho48fed632017-03-16 12:05:30 +0000726 setUniformValuesFromBindingQualifiers();
727
Jamie Madill4a3c2342015-10-08 12:58:45 -0400728 gatherInterfaceBlockInfo();
Jamie Madillccdf74b2015-08-18 10:46:12 -0400729
Martin Radev4c4c8e72016-08-04 12:25:34 +0300730 return NoError();
apatrick@chromium.org9a30b092012-06-06 20:21:55 +0000731}
732
daniel@transgaming.comaa5e59b2011-10-04 18:43:12 +0000733// Returns the program object to an unlinked state, before re-linking, or at destruction
Jamie Madill6c1f6712017-02-14 19:08:04 -0500734void Program::unlink()
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
Jamie Madill48ef11b2016-04-27 15:21:52 -0400736 mState.mAttributes.clear();
737 mState.mActiveAttribLocationsMask.reset();
738 mState.mTransformFeedbackVaryingVars.clear();
739 mState.mUniforms.clear();
740 mState.mUniformLocations.clear();
741 mState.mUniformBlocks.clear();
742 mState.mOutputVariables.clear();
jchen1015015f72017-03-16 13:54:21 +0800743 mState.mOutputLocations.clear();
Martin Radev4c4c8e72016-08-04 12:25:34 +0300744 mState.mComputeShaderLocalSize.fill(1);
Jamie Madille7d84322017-01-10 18:21:59 -0500745 mState.mSamplerBindings.clear();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500746
Geoff Lang7dd2e102014-11-10 15:19:26 -0500747 mValidated = false;
748
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000749 mLinked = false;
750}
751
Geoff Lange1a27752015-10-05 13:16:04 -0400752bool Program::isLinked() const
daniel@transgaming.com716056c2012-07-24 18:38:59 +0000753{
754 return mLinked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755}
756
Jamie Madilla2c74982016-12-12 11:20:42 -0500757Error Program::loadBinary(const Context *context,
758 GLenum binaryFormat,
759 const void *binary,
760 GLsizei length)
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +0000761{
Jamie Madill6c1f6712017-02-14 19:08:04 -0500762 unlink();
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000763
Geoff Lang7dd2e102014-11-10 15:19:26 -0500764#if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED
He Yunchaoacd18982017-01-04 10:46:42 +0800765 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500766#else
Geoff Langc46cc2f2015-10-01 17:16:20 -0400767 ASSERT(binaryFormat == GL_PROGRAM_BINARY_ANGLE);
768 if (binaryFormat != GL_PROGRAM_BINARY_ANGLE)
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000769 {
Jamie Madillf6113162015-05-07 11:49:21 -0400770 mInfoLog << "Invalid program binary format.";
He Yunchaoacd18982017-01-04 10:46:42 +0800771 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500772 }
773
Geoff Langc46cc2f2015-10-01 17:16:20 -0400774 BinaryInputStream stream(binary, length);
775
Jamie Madilla2c74982016-12-12 11:20:42 -0500776 unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
777 stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
778 if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) !=
779 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -0500780 {
Jamie Madillf6113162015-05-07 11:49:21 -0400781 mInfoLog << "Invalid program binary version.";
He Yunchaoacd18982017-01-04 10:46:42 +0800782 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500783 }
784
Jamie Madilla2c74982016-12-12 11:20:42 -0500785 int majorVersion = stream.readInt<int>();
786 int minorVersion = stream.readInt<int>();
787 if (majorVersion != context->getClientMajorVersion() ||
788 minorVersion != context->getClientMinorVersion())
Geoff Lang7dd2e102014-11-10 15:19:26 -0500789 {
Jamie Madilla2c74982016-12-12 11:20:42 -0500790 mInfoLog << "Cannot load program binaries across different ES context versions.";
He Yunchaoacd18982017-01-04 10:46:42 +0800791 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500792 }
793
Martin Radev4c4c8e72016-08-04 12:25:34 +0300794 mState.mComputeShaderLocalSize[0] = stream.readInt<int>();
795 mState.mComputeShaderLocalSize[1] = stream.readInt<int>();
796 mState.mComputeShaderLocalSize[2] = stream.readInt<int>();
797
Jamie Madill63805b42015-08-25 13:17:39 -0400798 static_assert(MAX_VERTEX_ATTRIBS <= sizeof(unsigned long) * 8,
799 "Too many vertex attribs for mask");
Jamie Madill48ef11b2016-04-27 15:21:52 -0400800 mState.mActiveAttribLocationsMask = stream.readInt<unsigned long>();
Geoff Lang7dd2e102014-11-10 15:19:26 -0500801
Jamie Madill3da79b72015-04-27 11:09:17 -0400802 unsigned int attribCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400803 ASSERT(mState.mAttributes.empty());
Jamie Madill3da79b72015-04-27 11:09:17 -0400804 for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex)
805 {
Jamie Madillc349ec02015-08-21 16:53:12 -0400806 sh::Attribute attrib;
Jamie Madill62d31cb2015-09-11 13:25:51 -0400807 LoadShaderVar(&stream, &attrib);
808 attrib.location = stream.readInt<int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400809 mState.mAttributes.push_back(attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400810 }
811
Jamie Madill62d31cb2015-09-11 13:25:51 -0400812 unsigned int uniformCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400813 ASSERT(mState.mUniforms.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400814 for (unsigned int uniformIndex = 0; uniformIndex < uniformCount; ++uniformIndex)
815 {
816 LinkedUniform uniform;
817 LoadShaderVar(&stream, &uniform);
818
819 uniform.blockIndex = stream.readInt<int>();
820 uniform.blockInfo.offset = stream.readInt<int>();
821 uniform.blockInfo.arrayStride = stream.readInt<int>();
822 uniform.blockInfo.matrixStride = stream.readInt<int>();
823 uniform.blockInfo.isRowMajorMatrix = stream.readBool();
824
Jamie Madill48ef11b2016-04-27 15:21:52 -0400825 mState.mUniforms.push_back(uniform);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400826 }
827
828 const unsigned int uniformIndexCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400829 ASSERT(mState.mUniformLocations.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400830 for (unsigned int uniformIndexIndex = 0; uniformIndexIndex < uniformIndexCount;
831 uniformIndexIndex++)
832 {
833 VariableLocation variable;
834 stream.readString(&variable.name);
835 stream.readInt(&variable.element);
836 stream.readInt(&variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400837 stream.readBool(&variable.used);
838 stream.readBool(&variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400839
Jamie Madill48ef11b2016-04-27 15:21:52 -0400840 mState.mUniformLocations.push_back(variable);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400841 }
842
843 unsigned int uniformBlockCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400844 ASSERT(mState.mUniformBlocks.empty());
Jamie Madill62d31cb2015-09-11 13:25:51 -0400845 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < uniformBlockCount;
846 ++uniformBlockIndex)
847 {
848 UniformBlock uniformBlock;
849 stream.readString(&uniformBlock.name);
850 stream.readBool(&uniformBlock.isArray);
851 stream.readInt(&uniformBlock.arrayElement);
852 stream.readInt(&uniformBlock.dataSize);
853 stream.readBool(&uniformBlock.vertexStaticUse);
854 stream.readBool(&uniformBlock.fragmentStaticUse);
855
856 unsigned int numMembers = stream.readInt<unsigned int>();
857 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numMembers; blockMemberIndex++)
858 {
859 uniformBlock.memberUniformIndexes.push_back(stream.readInt<unsigned int>());
860 }
861
Jamie Madill48ef11b2016-04-27 15:21:52 -0400862 mState.mUniformBlocks.push_back(uniformBlock);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400863 }
864
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500865 for (GLuint bindingIndex = 0; bindingIndex < mState.mUniformBlockBindings.size();
866 ++bindingIndex)
867 {
868 stream.readInt(&mState.mUniformBlockBindings[bindingIndex]);
869 mState.mActiveUniformBlockBindings.set(bindingIndex,
870 mState.mUniformBlockBindings[bindingIndex] != 0);
871 }
872
Brandon Jones1048ea72015-10-06 15:34:52 -0700873 unsigned int transformFeedbackVaryingCount = stream.readInt<unsigned int>();
Jamie Madill48ef11b2016-04-27 15:21:52 -0400874 ASSERT(mState.mTransformFeedbackVaryingVars.empty());
Brandon Jones1048ea72015-10-06 15:34:52 -0700875 for (unsigned int transformFeedbackVaryingIndex = 0;
876 transformFeedbackVaryingIndex < transformFeedbackVaryingCount;
877 ++transformFeedbackVaryingIndex)
878 {
879 sh::Varying varying;
880 stream.readInt(&varying.arraySize);
881 stream.readInt(&varying.type);
882 stream.readString(&varying.name);
883
Jamie Madill48ef11b2016-04-27 15:21:52 -0400884 mState.mTransformFeedbackVaryingVars.push_back(varying);
Brandon Jones1048ea72015-10-06 15:34:52 -0700885 }
886
Jamie Madill48ef11b2016-04-27 15:21:52 -0400887 stream.readInt(&mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -0400888
jchen1015015f72017-03-16 13:54:21 +0800889 unsigned int outputCount = stream.readInt<unsigned int>();
890 ASSERT(mState.mOutputVariables.empty());
891 for (unsigned int outputIndex = 0; outputIndex < outputCount; ++outputIndex)
892 {
893 sh::OutputVariable output;
894 LoadShaderVar(&stream, &output);
895 output.location = stream.readInt<int>();
896 mState.mOutputVariables.push_back(output);
897 }
898
Jamie Madill80a6fc02015-08-21 16:53:16 -0400899 unsigned int outputVarCount = stream.readInt<unsigned int>();
900 for (unsigned int outputIndex = 0; outputIndex < outputVarCount; ++outputIndex)
901 {
902 int locationIndex = stream.readInt<int>();
903 VariableLocation locationData;
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400904 stream.readInt(&locationData.element);
905 stream.readInt(&locationData.index);
906 stream.readString(&locationData.name);
jchen1015015f72017-03-16 13:54:21 +0800907 mState.mOutputLocations[locationIndex] = locationData;
Jamie Madill80a6fc02015-08-21 16:53:16 -0400908 }
909
Jamie Madille7d84322017-01-10 18:21:59 -0500910 stream.readInt(&mState.mSamplerUniformRange.start);
911 stream.readInt(&mState.mSamplerUniformRange.end);
912
913 unsigned int samplerCount = stream.readInt<unsigned int>();
914 for (unsigned int samplerIndex = 0; samplerIndex < samplerCount; ++samplerIndex)
915 {
916 GLenum textureType = stream.readInt<GLenum>();
917 size_t bindingCount = stream.readInt<size_t>();
918 mState.mSamplerBindings.emplace_back(SamplerBinding(textureType, bindingCount));
919 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400920
Jamie Madilla7d12dc2016-12-13 15:08:19 -0500921 ANGLE_TRY_RESULT(mProgram->load(context->getImplementation(), mInfoLog, &stream), mLinked);
daniel@transgaming.com4c962bf2012-07-24 18:37:02 +0000922
Jamie Madillb0a838b2016-11-13 20:02:12 -0500923 return NoError();
Jamie Madilla2c74982016-12-12 11:20:42 -0500924#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
Geoff Lang7dd2e102014-11-10 15:19:26 -0500925}
926
Jamie Madilla2c74982016-12-12 11:20:42 -0500927Error Program::saveBinary(const Context *context,
928 GLenum *binaryFormat,
929 void *binary,
930 GLsizei bufSize,
931 GLsizei *length) const
Geoff Lang7dd2e102014-11-10 15:19:26 -0500932{
933 if (binaryFormat)
934 {
Geoff Langc46cc2f2015-10-01 17:16:20 -0400935 *binaryFormat = GL_PROGRAM_BINARY_ANGLE;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500936 }
937
938 BinaryOutputStream stream;
939
Geoff Lang7dd2e102014-11-10 15:19:26 -0500940 stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
941
Jamie Madilla2c74982016-12-12 11:20:42 -0500942 // nullptr context is supported when computing binary length.
943 if (context)
944 {
945 stream.writeInt(context->getClientVersion().major);
946 stream.writeInt(context->getClientVersion().minor);
947 }
948 else
949 {
950 stream.writeInt(2);
951 stream.writeInt(0);
952 }
953
Martin Radev4c4c8e72016-08-04 12:25:34 +0300954 stream.writeInt(mState.mComputeShaderLocalSize[0]);
955 stream.writeInt(mState.mComputeShaderLocalSize[1]);
956 stream.writeInt(mState.mComputeShaderLocalSize[2]);
957
Jamie Madill48ef11b2016-04-27 15:21:52 -0400958 stream.writeInt(mState.mActiveAttribLocationsMask.to_ulong());
Geoff Lang7dd2e102014-11-10 15:19:26 -0500959
Jamie Madill48ef11b2016-04-27 15:21:52 -0400960 stream.writeInt(mState.mAttributes.size());
961 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madill3da79b72015-04-27 11:09:17 -0400962 {
Jamie Madill62d31cb2015-09-11 13:25:51 -0400963 WriteShaderVar(&stream, attrib);
Jamie Madill3da79b72015-04-27 11:09:17 -0400964 stream.writeInt(attrib.location);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400965 }
966
Jamie Madill48ef11b2016-04-27 15:21:52 -0400967 stream.writeInt(mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -0500968 for (const LinkedUniform &uniform : mState.mUniforms)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400969 {
970 WriteShaderVar(&stream, uniform);
971
972 // FIXME: referenced
973
974 stream.writeInt(uniform.blockIndex);
975 stream.writeInt(uniform.blockInfo.offset);
976 stream.writeInt(uniform.blockInfo.arrayStride);
977 stream.writeInt(uniform.blockInfo.matrixStride);
978 stream.writeInt(uniform.blockInfo.isRowMajorMatrix);
979 }
980
Jamie Madill48ef11b2016-04-27 15:21:52 -0400981 stream.writeInt(mState.mUniformLocations.size());
982 for (const auto &variable : mState.mUniformLocations)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400983 {
984 stream.writeString(variable.name);
985 stream.writeInt(variable.element);
986 stream.writeInt(variable.index);
Geoff Langd8605522016-04-13 10:19:12 -0400987 stream.writeInt(variable.used);
988 stream.writeInt(variable.ignored);
Jamie Madill62d31cb2015-09-11 13:25:51 -0400989 }
990
Jamie Madill48ef11b2016-04-27 15:21:52 -0400991 stream.writeInt(mState.mUniformBlocks.size());
992 for (const UniformBlock &uniformBlock : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -0400993 {
994 stream.writeString(uniformBlock.name);
995 stream.writeInt(uniformBlock.isArray);
996 stream.writeInt(uniformBlock.arrayElement);
997 stream.writeInt(uniformBlock.dataSize);
998
999 stream.writeInt(uniformBlock.vertexStaticUse);
1000 stream.writeInt(uniformBlock.fragmentStaticUse);
1001
1002 stream.writeInt(uniformBlock.memberUniformIndexes.size());
1003 for (unsigned int memberUniformIndex : uniformBlock.memberUniformIndexes)
1004 {
1005 stream.writeInt(memberUniformIndex);
1006 }
Jamie Madill3da79b72015-04-27 11:09:17 -04001007 }
1008
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001009 for (GLuint binding : mState.mUniformBlockBindings)
1010 {
1011 stream.writeInt(binding);
1012 }
1013
Jamie Madill48ef11b2016-04-27 15:21:52 -04001014 stream.writeInt(mState.mTransformFeedbackVaryingVars.size());
1015 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Brandon Jones1048ea72015-10-06 15:34:52 -07001016 {
1017 stream.writeInt(varying.arraySize);
1018 stream.writeInt(varying.type);
1019 stream.writeString(varying.name);
1020 }
1021
Jamie Madill48ef11b2016-04-27 15:21:52 -04001022 stream.writeInt(mState.mTransformFeedbackBufferMode);
Jamie Madillada9ecc2015-08-17 12:53:37 -04001023
Jamie Madill48ef11b2016-04-27 15:21:52 -04001024 stream.writeInt(mState.mOutputVariables.size());
jchen1015015f72017-03-16 13:54:21 +08001025 for (const sh::OutputVariable &output : mState.mOutputVariables)
1026 {
1027 WriteShaderVar(&stream, output);
1028 stream.writeInt(output.location);
1029 }
1030
1031 stream.writeInt(mState.mOutputLocations.size());
1032 for (const auto &outputPair : mState.mOutputLocations)
Jamie Madill80a6fc02015-08-21 16:53:16 -04001033 {
1034 stream.writeInt(outputPair.first);
Jamie Madille2e406c2016-06-02 13:04:10 -04001035 stream.writeIntOrNegOne(outputPair.second.element);
Jamie Madill80a6fc02015-08-21 16:53:16 -04001036 stream.writeInt(outputPair.second.index);
1037 stream.writeString(outputPair.second.name);
1038 }
1039
Jamie Madille7d84322017-01-10 18:21:59 -05001040 stream.writeInt(mState.mSamplerUniformRange.start);
1041 stream.writeInt(mState.mSamplerUniformRange.end);
1042
1043 stream.writeInt(mState.mSamplerBindings.size());
1044 for (const auto &samplerBinding : mState.mSamplerBindings)
1045 {
1046 stream.writeInt(samplerBinding.textureType);
1047 stream.writeInt(samplerBinding.boundTextureUnits.size());
1048 }
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001049
Jamie Madilla2c74982016-12-12 11:20:42 -05001050 ANGLE_TRY(mProgram->save(&stream));
Geoff Lang7dd2e102014-11-10 15:19:26 -05001051
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001052 GLsizei streamLength = static_cast<GLsizei>(stream.length());
Jamie Madill48ef11b2016-04-27 15:21:52 -04001053 const void *streamState = stream.data();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001054
1055 if (streamLength > bufSize)
1056 {
1057 if (length)
1058 {
1059 *length = 0;
1060 }
1061
1062 // TODO: This should be moved to the validation layer but computing the size of the binary before saving
1063 // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
1064 // sizes and then copy it.
1065 return Error(GL_INVALID_OPERATION);
1066 }
1067
1068 if (binary)
1069 {
1070 char *ptr = reinterpret_cast<char*>(binary);
1071
Jamie Madill48ef11b2016-04-27 15:21:52 -04001072 memcpy(ptr, streamState, streamLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001073 ptr += streamLength;
1074
1075 ASSERT(ptr - streamLength == binary);
1076 }
1077
1078 if (length)
1079 {
1080 *length = streamLength;
1081 }
1082
He Yunchaoacd18982017-01-04 10:46:42 +08001083 return NoError();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001084}
1085
1086GLint Program::getBinaryLength() const
1087{
1088 GLint length;
Jamie Madilla2c74982016-12-12 11:20:42 -05001089 Error error = saveBinary(nullptr, nullptr, nullptr, std::numeric_limits<GLint>::max(), &length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001090 if (error.isError())
1091 {
1092 return 0;
1093 }
1094
1095 return length;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001096}
1097
Geoff Langc5629752015-12-07 16:29:04 -05001098void Program::setBinaryRetrievableHint(bool retrievable)
1099{
1100 // TODO(jmadill) : replace with dirty bits
1101 mProgram->setBinaryRetrievableHint(retrievable);
Jamie Madill48ef11b2016-04-27 15:21:52 -04001102 mState.mBinaryRetrieveableHint = retrievable;
Geoff Langc5629752015-12-07 16:29:04 -05001103}
1104
1105bool Program::getBinaryRetrievableHint() const
1106{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001107 return mState.mBinaryRetrieveableHint;
Geoff Langc5629752015-12-07 16:29:04 -05001108}
1109
Yunchao He61afff12017-03-14 15:34:03 +08001110void Program::setSeparable(bool separable)
1111{
1112 // TODO(yunchao) : replace with dirty bits
1113 if (mState.mSeparable != separable)
1114 {
1115 mProgram->setSeparable(separable);
1116 mState.mSeparable = separable;
1117 }
1118}
1119
1120bool Program::isSeparable() const
1121{
1122 return mState.mSeparable;
1123}
1124
Jamie Madill6c1f6712017-02-14 19:08:04 -05001125void Program::release(const Context *context)
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001126{
1127 mRefCount--;
1128
1129 if (mRefCount == 0 && mDeleteStatus)
1130 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001131 mResourceManager->deleteProgram(context, mHandle);
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001132 }
1133}
1134
1135void Program::addRef()
1136{
1137 mRefCount++;
1138}
1139
1140unsigned int Program::getRefCount() const
1141{
1142 return mRefCount;
1143}
1144
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001145int Program::getInfoLogLength() const
1146{
Jamie Madill71c3b2c2015-05-07 11:49:20 -04001147 return static_cast<int>(mInfoLog.getLength());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001148}
1149
Geoff Lange1a27752015-10-05 13:16:04 -04001150void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001151{
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001152 return mInfoLog.getLog(bufSize, length, infoLog);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001153}
1154
Geoff Lange1a27752015-10-05 13:16:04 -04001155void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) const
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001156{
1157 int total = 0;
1158
Martin Radev4c4c8e72016-08-04 12:25:34 +03001159 if (mState.mAttachedComputeShader)
1160 {
1161 if (total < maxCount)
1162 {
1163 shaders[total] = mState.mAttachedComputeShader->getHandle();
1164 total++;
1165 }
1166 }
1167
Jamie Madill48ef11b2016-04-27 15:21:52 -04001168 if (mState.mAttachedVertexShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001169 {
1170 if (total < maxCount)
1171 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001172 shaders[total] = mState.mAttachedVertexShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001173 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001174 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001175 }
1176
Jamie Madill48ef11b2016-04-27 15:21:52 -04001177 if (mState.mAttachedFragmentShader)
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001178 {
1179 if (total < maxCount)
1180 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001181 shaders[total] = mState.mAttachedFragmentShader->getHandle();
Olli Etuaho586bc552016-03-04 11:46:03 +02001182 total++;
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001183 }
daniel@transgaming.com6c785212010-03-30 03:36:17 +00001184 }
1185
1186 if (count)
1187 {
1188 *count = total;
1189 }
1190}
1191
Geoff Lange1a27752015-10-05 13:16:04 -04001192GLuint Program::getAttributeLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001193{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001194 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001195 {
jchen1036e120e2017-03-14 14:53:58 +08001196 if (attribute.name == name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001197 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001198 return attribute.location;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001199 }
1200 }
1201
Austin Kinrossb8af7232015-03-16 22:33:25 -07001202 return static_cast<GLuint>(-1);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001203}
1204
Jamie Madill63805b42015-08-25 13:17:39 -04001205bool Program::isAttribLocationActive(size_t attribLocation) const
Jamie Madill56c6e3c2015-04-15 10:18:05 -04001206{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001207 ASSERT(attribLocation < mState.mActiveAttribLocationsMask.size());
1208 return mState.mActiveAttribLocationsMask[attribLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001209}
1210
jchen10fd7c3b52017-03-21 15:36:03 +08001211void Program::getActiveAttribute(GLuint index,
1212 GLsizei bufsize,
1213 GLsizei *length,
1214 GLint *size,
1215 GLenum *type,
1216 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001217{
Jamie Madillc349ec02015-08-21 16:53:12 -04001218 if (!mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001219 {
1220 if (bufsize > 0)
1221 {
1222 name[0] = '\0';
1223 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001224
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001225 if (length)
1226 {
1227 *length = 0;
1228 }
1229
1230 *type = GL_NONE;
1231 *size = 1;
Jamie Madillc349ec02015-08-21 16:53:12 -04001232 return;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001233 }
Jamie Madillc349ec02015-08-21 16:53:12 -04001234
jchen1036e120e2017-03-14 14:53:58 +08001235 ASSERT(index < mState.mAttributes.size());
1236 const sh::Attribute &attrib = mState.mAttributes[index];
Jamie Madillc349ec02015-08-21 16:53:12 -04001237
1238 if (bufsize > 0)
1239 {
jchen10fd7c3b52017-03-21 15:36:03 +08001240 CopyStringToBuffer(name, attrib.name, bufsize, length);
Jamie Madillc349ec02015-08-21 16:53:12 -04001241 }
1242
1243 // Always a single 'type' instance
1244 *size = 1;
1245 *type = attrib.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001246}
1247
Geoff Lange1a27752015-10-05 13:16:04 -04001248GLint Program::getActiveAttributeCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001249{
Jamie Madillc349ec02015-08-21 16:53:12 -04001250 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001251 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001252 return 0;
1253 }
1254
jchen1036e120e2017-03-14 14:53:58 +08001255 return static_cast<GLint>(mState.mAttributes.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001256}
1257
Geoff Lange1a27752015-10-05 13:16:04 -04001258GLint Program::getActiveAttributeMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001259{
Jamie Madillc349ec02015-08-21 16:53:12 -04001260 if (!mLinked)
Jamie Madill2d773182015-08-18 10:27:28 -04001261 {
Jamie Madillc349ec02015-08-21 16:53:12 -04001262 return 0;
1263 }
1264
1265 size_t maxLength = 0;
1266
Jamie Madill48ef11b2016-04-27 15:21:52 -04001267 for (const sh::Attribute &attrib : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04001268 {
jchen1036e120e2017-03-14 14:53:58 +08001269 maxLength = std::max(attrib.name.length() + 1, maxLength);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001270 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001271
Jamie Madillc349ec02015-08-21 16:53:12 -04001272 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001273}
1274
jchen1015015f72017-03-16 13:54:21 +08001275GLuint Program::getInputResourceIndex(const GLchar *name) const
1276{
1277 for (GLuint attributeIndex = 0; attributeIndex < mState.mAttributes.size(); ++attributeIndex)
1278 {
1279 const sh::Attribute &attribute = mState.mAttributes[attributeIndex];
1280 if (attribute.name == name)
1281 {
1282 return attributeIndex;
1283 }
1284 }
1285 return GL_INVALID_INDEX;
1286}
1287
1288GLuint Program::getOutputResourceIndex(const GLchar *name) const
1289{
1290 return GetResourceIndexFromName(mState.mOutputVariables, std::string(name));
1291}
1292
jchen10fd7c3b52017-03-21 15:36:03 +08001293size_t Program::getOutputResourceCount() const
1294{
1295 return (mLinked ? mState.mOutputVariables.size() : 0);
1296}
1297
1298void Program::getInputResourceName(GLuint index,
1299 GLsizei bufSize,
1300 GLsizei *length,
1301 GLchar *name) const
1302{
1303 GLint size;
1304 GLenum type;
1305 getActiveAttribute(index, bufSize, length, &size, &type, name);
1306}
1307
1308void Program::getOutputResourceName(GLuint index,
1309 GLsizei bufSize,
1310 GLsizei *length,
1311 GLchar *name) const
1312{
1313 if (length)
1314 {
1315 *length = 0;
1316 }
1317
1318 if (!mLinked)
1319 {
1320 if (bufSize > 0)
1321 {
1322 name[0] = '\0';
1323 }
1324 return;
1325 }
1326 ASSERT(index < mState.mOutputVariables.size());
1327 const auto &output = mState.mOutputVariables[index];
1328
1329 if (bufSize > 0)
1330 {
1331 std::string nameWithArray = (output.isArray() ? output.name + "[0]" : output.name);
1332
1333 CopyStringToBuffer(name, nameWithArray, bufSize, length);
1334 }
1335}
1336
Geoff Lang7dd2e102014-11-10 15:19:26 -05001337GLint Program::getFragDataLocation(const std::string &name) const
1338{
1339 std::string baseName(name);
1340 unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName);
jchen1015015f72017-03-16 13:54:21 +08001341 for (auto outputPair : mState.mOutputLocations)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001342 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001343 const VariableLocation &outputVariable = outputPair.second;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001344 if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
1345 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04001346 return static_cast<GLint>(outputPair.first);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001347 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001348 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001349 return -1;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001350}
1351
Geoff Lange1a27752015-10-05 13:16:04 -04001352void Program::getActiveUniform(GLuint index,
1353 GLsizei bufsize,
1354 GLsizei *length,
1355 GLint *size,
1356 GLenum *type,
1357 GLchar *name) const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001358{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001359 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001360 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001361 // index must be smaller than getActiveUniformCount()
Jamie Madill48ef11b2016-04-27 15:21:52 -04001362 ASSERT(index < mState.mUniforms.size());
1363 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001364
1365 if (bufsize > 0)
1366 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001367 std::string string = uniform.name;
1368 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001369 {
1370 string += "[0]";
1371 }
jchen10fd7c3b52017-03-21 15:36:03 +08001372 CopyStringToBuffer(name, string, bufsize, length);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001373 }
1374
Jamie Madill62d31cb2015-09-11 13:25:51 -04001375 *size = uniform.elementCount();
1376 *type = uniform.type;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001377 }
1378 else
1379 {
1380 if (bufsize > 0)
1381 {
1382 name[0] = '\0';
1383 }
1384
1385 if (length)
1386 {
1387 *length = 0;
1388 }
1389
1390 *size = 0;
1391 *type = GL_NONE;
1392 }
1393}
1394
Geoff Lange1a27752015-10-05 13:16:04 -04001395GLint Program::getActiveUniformCount() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001396{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001397 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001398 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001399 return static_cast<GLint>(mState.mUniforms.size());
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001400 }
1401 else
1402 {
1403 return 0;
1404 }
1405}
1406
Geoff Lange1a27752015-10-05 13:16:04 -04001407GLint Program::getActiveUniformMaxLength() const
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001408{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001409 size_t maxLength = 0;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001410
1411 if (mLinked)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001412 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001413 for (const LinkedUniform &uniform : mState.mUniforms)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001414 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001415 if (!uniform.name.empty())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001416 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001417 size_t length = uniform.name.length() + 1u;
1418 if (uniform.isArray())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001419 {
1420 length += 3; // Counting in "[0]".
1421 }
1422 maxLength = std::max(length, maxLength);
1423 }
1424 }
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001425 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001426
Jamie Madill62d31cb2015-09-11 13:25:51 -04001427 return static_cast<GLint>(maxLength);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001428}
1429
1430GLint Program::getActiveUniformi(GLuint index, GLenum pname) const
1431{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001432 ASSERT(static_cast<size_t>(index) < mState.mUniforms.size());
Jamie Madilla2c74982016-12-12 11:20:42 -05001433 const LinkedUniform &uniform = mState.mUniforms[index];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001434 switch (pname)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001435 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001436 case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
1437 case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
1438 case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
1439 case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
1440 case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
1441 case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
1442 case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
1443 case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
1444 default:
1445 UNREACHABLE();
1446 break;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001447 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001448 return 0;
1449}
1450
1451bool Program::isValidUniformLocation(GLint location) const
1452{
Jamie Madille2e406c2016-06-02 13:04:10 -04001453 ASSERT(angle::IsValueInRangeForNumericType<GLint>(mState.mUniformLocations.size()));
Jamie Madill48ef11b2016-04-27 15:21:52 -04001454 return (location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size() &&
1455 mState.mUniformLocations[static_cast<size_t>(location)].used);
Geoff Langd8605522016-04-13 10:19:12 -04001456}
1457
Jamie Madill62d31cb2015-09-11 13:25:51 -04001458const LinkedUniform &Program::getUniformByLocation(GLint location) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001459{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001460 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
Jamie Madille7d84322017-01-10 18:21:59 -05001461 return mState.mUniforms[mState.getUniformIndexFromLocation(location)];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001462}
1463
Jamie Madillac4e9c32017-01-13 14:07:12 -05001464const VariableLocation &Program::getUniformLocation(GLint location) const
1465{
1466 ASSERT(location >= 0 && static_cast<size_t>(location) < mState.mUniformLocations.size());
1467 return mState.mUniformLocations[location];
1468}
1469
1470const std::vector<VariableLocation> &Program::getUniformLocations() const
1471{
1472 return mState.mUniformLocations;
1473}
1474
1475const LinkedUniform &Program::getUniformByIndex(GLuint index) const
1476{
1477 ASSERT(index < static_cast<size_t>(mState.mUniforms.size()));
1478 return mState.mUniforms[index];
1479}
1480
Jamie Madill62d31cb2015-09-11 13:25:51 -04001481GLint Program::getUniformLocation(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001482{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001483 return mState.getUniformLocation(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001484}
1485
Jamie Madill62d31cb2015-09-11 13:25:51 -04001486GLuint Program::getUniformIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001487{
Jamie Madille7d84322017-01-10 18:21:59 -05001488 return mState.getUniformIndexFromName(name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001489}
1490
1491void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
1492{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001493 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1494 mProgram->setUniform1fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001495}
1496
1497void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
1498{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001499 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1500 mProgram->setUniform2fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001501}
1502
1503void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
1504{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001505 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1506 mProgram->setUniform3fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001507}
1508
1509void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
1510{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001511 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1512 mProgram->setUniform4fv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001513}
1514
1515void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v)
1516{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001517 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1518 mProgram->setUniform1iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001519}
1520
1521void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v)
1522{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001523 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1524 mProgram->setUniform2iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001525}
1526
1527void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v)
1528{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001529 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1530 mProgram->setUniform3iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001531}
1532
1533void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v)
1534{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001535 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1536 mProgram->setUniform4iv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001537}
1538
1539void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
1540{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001541 GLsizei clampedCount = setUniformInternal(location, count, 1, v);
1542 mProgram->setUniform1uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001543}
1544
1545void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
1546{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001547 GLsizei clampedCount = setUniformInternal(location, count, 2, v);
1548 mProgram->setUniform2uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001549}
1550
1551void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
1552{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001553 GLsizei clampedCount = setUniformInternal(location, count, 3, v);
1554 mProgram->setUniform3uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001555}
1556
1557void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
1558{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001559 GLsizei clampedCount = setUniformInternal(location, count, 4, v);
1560 mProgram->setUniform4uiv(location, clampedCount, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001561}
1562
1563void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1564{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001565 GLsizei clampedCount = setMatrixUniformInternal<2, 2>(location, count, transpose, v);
1566 mProgram->setUniformMatrix2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001567}
1568
1569void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1570{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001571 GLsizei clampedCount = setMatrixUniformInternal<3, 3>(location, count, transpose, v);
1572 mProgram->setUniformMatrix3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001573}
1574
1575void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1576{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001577 GLsizei clampedCount = setMatrixUniformInternal<4, 4>(location, count, transpose, v);
1578 mProgram->setUniformMatrix4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001579}
1580
1581void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1582{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001583 GLsizei clampedCount = setMatrixUniformInternal<2, 3>(location, count, transpose, v);
1584 mProgram->setUniformMatrix2x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001585}
1586
1587void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1588{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001589 GLsizei clampedCount = setMatrixUniformInternal<2, 4>(location, count, transpose, v);
1590 mProgram->setUniformMatrix2x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001591}
1592
1593void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1594{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001595 GLsizei clampedCount = setMatrixUniformInternal<3, 2>(location, count, transpose, v);
1596 mProgram->setUniformMatrix3x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001597}
1598
1599void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1600{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001601 GLsizei clampedCount = setMatrixUniformInternal<3, 4>(location, count, transpose, v);
1602 mProgram->setUniformMatrix3x4fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001603}
1604
1605void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1606{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001607 GLsizei clampedCount = setMatrixUniformInternal<4, 2>(location, count, transpose, v);
1608 mProgram->setUniformMatrix4x2fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001609}
1610
1611void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v)
1612{
Corentin Wallez8b7d8142016-11-15 13:40:37 -05001613 GLsizei clampedCount = setMatrixUniformInternal<4, 3>(location, count, transpose, v);
1614 mProgram->setUniformMatrix4x3fv(location, clampedCount, transpose, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001615}
1616
Geoff Lange1a27752015-10-05 13:16:04 -04001617void Program::getUniformfv(GLint location, GLfloat *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001618{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001619 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001620}
1621
Geoff Lange1a27752015-10-05 13:16:04 -04001622void Program::getUniformiv(GLint location, GLint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001623{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001624 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001625}
1626
Geoff Lange1a27752015-10-05 13:16:04 -04001627void Program::getUniformuiv(GLint location, GLuint *v) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001628{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001629 getUniformInternal(location, v);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001630}
1631
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001632void Program::flagForDeletion()
1633{
1634 mDeleteStatus = true;
1635}
1636
1637bool Program::isFlaggedForDeletion() const
1638{
1639 return mDeleteStatus;
1640}
daniel@transgaming.com86a7a132010-04-29 03:32:32 +00001641
Brandon Jones43a53e22014-08-28 16:23:22 -07001642void Program::validate(const Caps &caps)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001643{
1644 mInfoLog.reset();
1645
Geoff Lang7dd2e102014-11-10 15:19:26 -05001646 if (mLinked)
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001647 {
Jamie Madill36cfd6a2015-08-18 10:46:20 -04001648 mValidated = (mProgram->validate(caps, &mInfoLog) == GL_TRUE);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001649 }
1650 else
1651 {
Jamie Madillf6113162015-05-07 11:49:21 -04001652 mInfoLog << "Program has not been successfully linked.";
apatrick@chromium.org253b8d22012-06-22 19:27:21 +00001653 }
1654}
1655
Geoff Lang7dd2e102014-11-10 15:19:26 -05001656bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps)
1657{
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001658 // Skip cache if we're using an infolog, so we get the full error.
1659 // Also skip the cache if the sample mapping has changed, or if we haven't ever validated.
1660 if (infoLog == nullptr && mCachedValidateSamplersResult.valid())
1661 {
1662 return mCachedValidateSamplersResult.value();
1663 }
1664
1665 if (mTextureUnitTypesCache.empty())
1666 {
1667 mTextureUnitTypesCache.resize(caps.maxCombinedTextureImageUnits, GL_NONE);
1668 }
1669 else
1670 {
1671 std::fill(mTextureUnitTypesCache.begin(), mTextureUnitTypesCache.end(), GL_NONE);
1672 }
1673
1674 // if any two active samplers in a program are of different types, but refer to the same
1675 // texture image unit, and this is the current program, then ValidateProgram will fail, and
1676 // DrawArrays and DrawElements will issue the INVALID_OPERATION error.
Jamie Madille7d84322017-01-10 18:21:59 -05001677 for (const auto &samplerBinding : mState.mSamplerBindings)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001678 {
Jamie Madille7d84322017-01-10 18:21:59 -05001679 GLenum textureType = samplerBinding.textureType;
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001680
Jamie Madille7d84322017-01-10 18:21:59 -05001681 for (GLuint textureUnit : samplerBinding.boundTextureUnits)
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001682 {
Jamie Madill3d3d2f22015-09-23 16:47:51 -04001683 if (textureUnit >= caps.maxCombinedTextureImageUnits)
1684 {
1685 if (infoLog)
1686 {
1687 (*infoLog) << "Sampler uniform (" << textureUnit
1688 << ") exceeds GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ("
1689 << caps.maxCombinedTextureImageUnits << ")";
1690 }
1691
1692 mCachedValidateSamplersResult = false;
1693 return false;
1694 }
1695
1696 if (mTextureUnitTypesCache[textureUnit] != GL_NONE)
1697 {
1698 if (textureType != mTextureUnitTypesCache[textureUnit])
1699 {
1700 if (infoLog)
1701 {
1702 (*infoLog) << "Samplers of conflicting types refer to the same texture "
1703 "image unit ("
1704 << textureUnit << ").";
1705 }
1706
1707 mCachedValidateSamplersResult = false;
1708 return false;
1709 }
1710 }
1711 else
1712 {
1713 mTextureUnitTypesCache[textureUnit] = textureType;
1714 }
1715 }
1716 }
1717
1718 mCachedValidateSamplersResult = true;
1719 return true;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001720}
1721
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001722bool Program::isValidated() const
1723{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001724 return mValidated;
1725}
1726
Geoff Lange1a27752015-10-05 13:16:04 -04001727GLuint Program::getActiveUniformBlockCount() const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001728{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001729 return static_cast<GLuint>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001730}
1731
1732void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
1733{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001734 ASSERT(
1735 uniformBlockIndex <
1736 mState.mUniformBlocks.size()); // index must be smaller than getActiveUniformBlockCount()
Geoff Lang7dd2e102014-11-10 15:19:26 -05001737
Jamie Madill48ef11b2016-04-27 15:21:52 -04001738 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001739
1740 if (bufSize > 0)
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001741 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001742 std::string string = uniformBlock.name;
1743
Jamie Madill62d31cb2015-09-11 13:25:51 -04001744 if (uniformBlock.isArray)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001745 {
Jamie Madill62d31cb2015-09-11 13:25:51 -04001746 string += ArrayString(uniformBlock.arrayElement);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001747 }
jchen10fd7c3b52017-03-21 15:36:03 +08001748 CopyStringToBuffer(uniformBlockName, string, bufSize, length);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001749 }
1750}
1751
Geoff Lange1a27752015-10-05 13:16:04 -04001752GLint Program::getActiveUniformBlockMaxLength() const
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001753{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001754 int maxLength = 0;
1755
1756 if (mLinked)
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001757 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001758 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05001759 for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
1760 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001761 const UniformBlock &uniformBlock = mState.mUniformBlocks[uniformBlockIndex];
Geoff Lang7dd2e102014-11-10 15:19:26 -05001762 if (!uniformBlock.name.empty())
1763 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001764 const int length = static_cast<int>(uniformBlock.name.length()) + 1;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001765
1766 // Counting in "[0]".
Jamie Madill62d31cb2015-09-11 13:25:51 -04001767 const int arrayLength = (uniformBlock.isArray ? 3 : 0);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001768
1769 maxLength = std::max(length + arrayLength, maxLength);
1770 }
1771 }
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001772 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001773
1774 return maxLength;
1775}
1776
Geoff Lange1a27752015-10-05 13:16:04 -04001777GLuint Program::getUniformBlockIndex(const std::string &name) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001778{
Jamie Madill62d31cb2015-09-11 13:25:51 -04001779 size_t subscript = GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08001780 std::string baseName = ParseResourceName(name, &subscript);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001781
Jamie Madill48ef11b2016-04-27 15:21:52 -04001782 unsigned int numUniformBlocks = static_cast<unsigned int>(mState.mUniformBlocks.size());
Jamie Madill62d31cb2015-09-11 13:25:51 -04001783 for (unsigned int blockIndex = 0; blockIndex < numUniformBlocks; blockIndex++)
1784 {
Jamie Madilla2c74982016-12-12 11:20:42 -05001785 const UniformBlock &uniformBlock = mState.mUniformBlocks[blockIndex];
Jamie Madill62d31cb2015-09-11 13:25:51 -04001786 if (uniformBlock.name == baseName)
1787 {
1788 const bool arrayElementZero =
1789 (subscript == GL_INVALID_INDEX &&
1790 (!uniformBlock.isArray || uniformBlock.arrayElement == 0));
1791 if (subscript == uniformBlock.arrayElement || arrayElementZero)
1792 {
1793 return blockIndex;
1794 }
1795 }
1796 }
1797
1798 return GL_INVALID_INDEX;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +00001799}
1800
Jamie Madill62d31cb2015-09-11 13:25:51 -04001801const UniformBlock &Program::getUniformBlockByIndex(GLuint index) const
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001802{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001803 ASSERT(index < static_cast<GLuint>(mState.mUniformBlocks.size()));
1804 return mState.mUniformBlocks[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001805}
1806
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001807void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
1808{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001809 mState.mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding;
Jamie Madilla7d12dc2016-12-13 15:08:19 -05001810 mState.mActiveUniformBlockBindings.set(uniformBlockIndex, uniformBlockBinding != 0);
Geoff Lang5d124a62015-09-15 13:03:27 -04001811 mProgram->setUniformBlockBinding(uniformBlockIndex, uniformBlockBinding);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001812}
1813
1814GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const
1815{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001816 return mState.getUniformBlockBinding(uniformBlockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001817}
1818
1819void Program::resetUniformBlockBindings()
1820{
1821 for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++)
1822 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001823 mState.mUniformBlockBindings[blockId] = 0;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001824 }
Jamie Madill48ef11b2016-04-27 15:21:52 -04001825 mState.mActiveUniformBlockBindings.reset();
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +00001826}
1827
Geoff Lang48dcae72014-02-05 16:28:24 -05001828void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode)
1829{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001830 mState.mTransformFeedbackVaryingNames.resize(count);
Geoff Lang48dcae72014-02-05 16:28:24 -05001831 for (GLsizei i = 0; i < count; i++)
1832 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001833 mState.mTransformFeedbackVaryingNames[i] = varyings[i];
Geoff Lang48dcae72014-02-05 16:28:24 -05001834 }
1835
Jamie Madill48ef11b2016-04-27 15:21:52 -04001836 mState.mTransformFeedbackBufferMode = bufferMode;
Geoff Lang48dcae72014-02-05 16:28:24 -05001837}
1838
1839void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const
1840{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001841 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001842 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001843 ASSERT(index < mState.mTransformFeedbackVaryingVars.size());
1844 const sh::Varying &varying = mState.mTransformFeedbackVaryingVars[index];
Geoff Lang48dcae72014-02-05 16:28:24 -05001845 GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length()));
1846 if (length)
1847 {
1848 *length = lastNameIdx;
1849 }
1850 if (size)
1851 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04001852 *size = varying.elementCount();
Geoff Lang48dcae72014-02-05 16:28:24 -05001853 }
1854 if (type)
1855 {
1856 *type = varying.type;
1857 }
1858 if (name)
1859 {
1860 memcpy(name, varying.name.c_str(), lastNameIdx);
1861 name[lastNameIdx] = '\0';
1862 }
1863 }
1864}
1865
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001866GLsizei Program::getTransformFeedbackVaryingCount() const
1867{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001868 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001869 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04001870 return static_cast<GLsizei>(mState.mTransformFeedbackVaryingVars.size());
Geoff Lang48dcae72014-02-05 16:28:24 -05001871 }
1872 else
1873 {
1874 return 0;
1875 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001876}
1877
1878GLsizei Program::getTransformFeedbackVaryingMaxLength() const
1879{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001880 if (mLinked)
Geoff Lang48dcae72014-02-05 16:28:24 -05001881 {
1882 GLsizei maxSize = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04001883 for (const sh::Varying &varying : mState.mTransformFeedbackVaryingVars)
Geoff Lang48dcae72014-02-05 16:28:24 -05001884 {
Geoff Lang48dcae72014-02-05 16:28:24 -05001885 maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1));
1886 }
1887
1888 return maxSize;
1889 }
1890 else
1891 {
1892 return 0;
1893 }
Geoff Lang1b6edcb2014-02-03 14:27:56 -05001894}
1895
1896GLenum Program::getTransformFeedbackBufferMode() const
1897{
Jamie Madill48ef11b2016-04-27 15:21:52 -04001898 return mState.mTransformFeedbackBufferMode;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001899}
1900
Jamie Madill192745a2016-12-22 15:58:21 -05001901bool Program::linkVaryings(InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05001902{
Jamie Madill192745a2016-12-22 15:58:21 -05001903 const Shader *vertexShader = mState.mAttachedVertexShader;
1904 const Shader *fragmentShader = mState.mAttachedFragmentShader;
1905
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001906 ASSERT(vertexShader->getShaderVersion() == fragmentShader->getShaderVersion());
1907
Jamie Madill4cff2472015-08-21 16:53:18 -04001908 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
1909 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
Geoff Lang7dd2e102014-11-10 15:19:26 -05001910
Sami Väisänen46eaa942016-06-29 10:26:37 +03001911 std::map<GLuint, std::string> staticFragmentInputLocations;
1912
Jamie Madill4cff2472015-08-21 16:53:18 -04001913 for (const sh::Varying &output : fragmentVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001914 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001915 bool matched = false;
1916
1917 // Built-in varyings obey special rules
Jamie Madillada9ecc2015-08-17 12:53:37 -04001918 if (output.isBuiltIn())
Geoff Lang7dd2e102014-11-10 15:19:26 -05001919 {
1920 continue;
1921 }
1922
Jamie Madill4cff2472015-08-21 16:53:18 -04001923 for (const sh::Varying &input : vertexVaryings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001924 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001925 if (output.name == input.name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001926 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001927 ASSERT(!input.isBuiltIn());
Yuly Novikova1f6dc92016-06-15 23:27:04 -04001928 if (!linkValidateVaryings(infoLog, output.name, input, output,
1929 vertexShader->getShaderVersion()))
Geoff Lang7dd2e102014-11-10 15:19:26 -05001930 {
1931 return false;
1932 }
1933
Geoff Lang7dd2e102014-11-10 15:19:26 -05001934 matched = true;
1935 break;
1936 }
1937 }
1938
1939 // We permit unmatched, unreferenced varyings
Jamie Madillada9ecc2015-08-17 12:53:37 -04001940 if (!matched && output.staticUse)
Geoff Lang7dd2e102014-11-10 15:19:26 -05001941 {
Jamie Madillada9ecc2015-08-17 12:53:37 -04001942 infoLog << "Fragment varying " << output.name << " does not match any vertex varying";
Geoff Lang7dd2e102014-11-10 15:19:26 -05001943 return false;
1944 }
Sami Väisänen46eaa942016-06-29 10:26:37 +03001945
1946 // Check for aliased path rendering input bindings (if any).
1947 // If more than one binding refer statically to the same
1948 // location the link must fail.
1949
1950 if (!output.staticUse)
1951 continue;
1952
1953 const auto inputBinding = mFragmentInputBindings.getBinding(output.name);
1954 if (inputBinding == -1)
1955 continue;
1956
1957 const auto it = staticFragmentInputLocations.find(inputBinding);
1958 if (it == std::end(staticFragmentInputLocations))
1959 {
1960 staticFragmentInputLocations.insert(std::make_pair(inputBinding, output.name));
1961 }
1962 else
1963 {
1964 infoLog << "Binding for fragment input " << output.name << " conflicts with "
1965 << it->second;
1966 return false;
1967 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05001968 }
1969
Yuly Novikov817232e2017-02-22 18:36:10 -05001970 if (!linkValidateBuiltInVaryings(infoLog))
1971 {
1972 return false;
1973 }
1974
Jamie Madillada9ecc2015-08-17 12:53:37 -04001975 // TODO(jmadill): verify no unmatched vertex varyings?
1976
Geoff Lang7dd2e102014-11-10 15:19:26 -05001977 return true;
1978}
1979
Olli Etuaho4a92ceb2017-02-19 17:51:24 +00001980bool Program::linkUniforms(InfoLog &infoLog,
1981 const Caps &caps,
1982 const Bindings &uniformLocationBindings)
Martin Radev4c4c8e72016-08-04 12:25:34 +03001983{
Olli Etuahob78707c2017-03-09 15:03:11 +00001984 UniformLinker linker(mState);
1985 if (!linker.link(infoLog, caps, uniformLocationBindings))
Jamie Madill62d31cb2015-09-11 13:25:51 -04001986 {
1987 return false;
1988 }
1989
Olli Etuahob78707c2017-03-09 15:03:11 +00001990 linker.getResults(&mState.mUniforms, &mState.mUniformLocations);
Jamie Madill62d31cb2015-09-11 13:25:51 -04001991
Olli Etuaho48fed632017-03-16 12:05:30 +00001992 linkSamplerBindings();
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001993
1994 return true;
1995}
1996
Olli Etuaho48fed632017-03-16 12:05:30 +00001997void Program::linkSamplerBindings()
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001998{
1999 mState.mSamplerUniformRange.end = static_cast<unsigned int>(mState.mUniforms.size());
2000 mState.mSamplerUniformRange.start = mState.mSamplerUniformRange.end;
2001 auto samplerIter = mState.mUniforms.rbegin();
2002 while (samplerIter != mState.mUniforms.rend() && samplerIter->isSampler())
2003 {
2004 --mState.mSamplerUniformRange.start;
2005 ++samplerIter;
2006 }
2007 // If uniform is a sampler type, insert it into the mSamplerBindings array.
2008 for (unsigned int samplerIndex = mState.mSamplerUniformRange.start;
2009 samplerIndex < mState.mUniforms.size(); ++samplerIndex)
2010 {
2011 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2012 GLenum textureType = SamplerTypeToTextureType(samplerUniform.type);
2013 mState.mSamplerBindings.emplace_back(
2014 SamplerBinding(textureType, samplerUniform.elementCount()));
2015 }
2016}
2017
Martin Radev4c4c8e72016-08-04 12:25:34 +03002018bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog,
2019 const std::string &uniformName,
2020 const sh::InterfaceBlockField &vertexUniform,
2021 const sh::InterfaceBlockField &fragmentUniform)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002022{
Jamie Madillc4c744222015-11-04 09:39:47 -05002023 // We don't validate precision on UBO fields. See resolution of Khronos bug 10287.
2024 if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, false))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002025 {
2026 return false;
2027 }
2028
2029 if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
2030 {
Jamie Madillf6113162015-05-07 11:49:21 -04002031 infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002032 return false;
2033 }
2034
2035 return true;
2036}
2037
Jamie Madilleb979bf2016-11-15 12:28:46 -05002038// Assigns locations to all attributes from the bindings and program locations.
2039bool Program::linkAttributes(const ContextState &data, InfoLog &infoLog)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002040{
Jamie Madilleb979bf2016-11-15 12:28:46 -05002041 const auto *vertexShader = mState.getAttachedVertexShader();
2042
Geoff Lang7dd2e102014-11-10 15:19:26 -05002043 unsigned int usedLocations = 0;
Jamie Madill48ef11b2016-04-27 15:21:52 -04002044 mState.mAttributes = vertexShader->getActiveAttributes();
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002045 GLuint maxAttribs = data.getCaps().maxVertexAttributes;
Jamie Madill3da79b72015-04-27 11:09:17 -04002046
2047 // TODO(jmadill): handle aliasing robustly
Jamie Madill48ef11b2016-04-27 15:21:52 -04002048 if (mState.mAttributes.size() > maxAttribs)
Jamie Madill3da79b72015-04-27 11:09:17 -04002049 {
Jamie Madillf6113162015-05-07 11:49:21 -04002050 infoLog << "Too many vertex attributes.";
Jamie Madill3da79b72015-04-27 11:09:17 -04002051 return false;
2052 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002053
Jamie Madilldfde6ab2016-06-09 07:07:18 -07002054 std::vector<sh::Attribute *> usedAttribMap(maxAttribs, nullptr);
Jamie Madill4e107222015-08-24 14:12:17 +00002055
Jamie Madillc349ec02015-08-21 16:53:12 -04002056 // Link attributes that have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002057 for (sh::Attribute &attribute : mState.mAttributes)
Jamie Madillc349ec02015-08-21 16:53:12 -04002058 {
Jamie Madilleb979bf2016-11-15 12:28:46 -05002059 int bindingLocation = mAttributeBindings.getBinding(attribute.name);
Jamie Madillc349ec02015-08-21 16:53:12 -04002060 if (attribute.location == -1 && bindingLocation != -1)
Jamie Madill2d773182015-08-18 10:27:28 -04002061 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002062 attribute.location = bindingLocation;
2063 }
2064
2065 if (attribute.location != -1)
2066 {
2067 // Location is set by glBindAttribLocation or by location layout qualifier
Jamie Madill63805b42015-08-25 13:17:39 -04002068 const int regs = VariableRegisterCount(attribute.type);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002069
Jamie Madill63805b42015-08-25 13:17:39 -04002070 if (static_cast<GLuint>(regs + attribute.location) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002071 {
Jamie Madillf6113162015-05-07 11:49:21 -04002072 infoLog << "Active attribute (" << attribute.name << ") at location "
Jamie Madillc349ec02015-08-21 16:53:12 -04002073 << attribute.location << " is too big to fit";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002074
2075 return false;
2076 }
2077
Jamie Madill63805b42015-08-25 13:17:39 -04002078 for (int reg = 0; reg < regs; reg++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002079 {
Jamie Madill63805b42015-08-25 13:17:39 -04002080 const int regLocation = attribute.location + reg;
2081 sh::ShaderVariable *linkedAttribute = usedAttribMap[regLocation];
Geoff Lang7dd2e102014-11-10 15:19:26 -05002082
2083 // In GLSL 3.00, attribute aliasing produces a link error
Jamie Madill3da79b72015-04-27 11:09:17 -04002084 // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug
Jamie Madillc349ec02015-08-21 16:53:12 -04002085 if (linkedAttribute)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002086 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002087 // TODO(jmadill): fix aliasing on ES2
2088 // if (mProgram->getShaderVersion() >= 300)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002089 {
Jamie Madill5c6b7bf2015-08-17 12:53:35 -04002090 infoLog << "Attribute '" << attribute.name << "' aliases attribute '"
Jamie Madill63805b42015-08-25 13:17:39 -04002091 << linkedAttribute->name << "' at location " << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002092 return false;
2093 }
2094 }
Jamie Madillc349ec02015-08-21 16:53:12 -04002095 else
2096 {
Jamie Madill63805b42015-08-25 13:17:39 -04002097 usedAttribMap[regLocation] = &attribute;
Jamie Madillc349ec02015-08-21 16:53:12 -04002098 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002099
Jamie Madill63805b42015-08-25 13:17:39 -04002100 usedLocations |= 1 << regLocation;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002101 }
2102 }
2103 }
2104
2105 // Link attributes that don't have a binding location
Jamie Madill48ef11b2016-04-27 15:21:52 -04002106 for (sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002107 {
Jamie Madillc349ec02015-08-21 16:53:12 -04002108 // Not set by glBindAttribLocation or by location layout qualifier
2109 if (attribute.location == -1)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002110 {
Jamie Madill63805b42015-08-25 13:17:39 -04002111 int regs = VariableRegisterCount(attribute.type);
2112 int availableIndex = AllocateFirstFreeBits(&usedLocations, regs, maxAttribs);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002113
Jamie Madill63805b42015-08-25 13:17:39 -04002114 if (availableIndex == -1 || static_cast<GLuint>(availableIndex + regs) > maxAttribs)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002115 {
Jamie Madillf6113162015-05-07 11:49:21 -04002116 infoLog << "Too many active attributes (" << attribute.name << ")";
Jamie Madillc349ec02015-08-21 16:53:12 -04002117 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002118 }
2119
Jamie Madillc349ec02015-08-21 16:53:12 -04002120 attribute.location = availableIndex;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002121 }
2122 }
2123
Jamie Madill48ef11b2016-04-27 15:21:52 -04002124 for (const sh::Attribute &attribute : mState.mAttributes)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002125 {
Jamie Madill63805b42015-08-25 13:17:39 -04002126 ASSERT(attribute.location != -1);
2127 int regs = VariableRegisterCount(attribute.type);
Jamie Madillc349ec02015-08-21 16:53:12 -04002128
Jamie Madill63805b42015-08-25 13:17:39 -04002129 for (int r = 0; r < regs; r++)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002130 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002131 mState.mActiveAttribLocationsMask.set(attribute.location + r);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002132 }
2133 }
2134
Geoff Lang7dd2e102014-11-10 15:19:26 -05002135 return true;
2136}
2137
Martin Radev4c4c8e72016-08-04 12:25:34 +03002138bool Program::validateUniformBlocksCount(GLuint maxUniformBlocks,
2139 const std::vector<sh::InterfaceBlock> &intefaceBlocks,
2140 const std::string &errorMessage,
2141 InfoLog &infoLog) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002142{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002143 GLuint blockCount = 0;
2144 for (const sh::InterfaceBlock &block : intefaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002145 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002146 if (block.staticUse || block.layout != sh::BLOCKLAYOUT_PACKED)
Jamie Madille473dee2015-08-18 14:49:01 -04002147 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002148 if (++blockCount > maxUniformBlocks)
Jamie Madille473dee2015-08-18 14:49:01 -04002149 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002150 infoLog << errorMessage << maxUniformBlocks << ")";
Jamie Madille473dee2015-08-18 14:49:01 -04002151 return false;
2152 }
2153 }
Geoff Lang7dd2e102014-11-10 15:19:26 -05002154 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002155 return true;
2156}
Jamie Madille473dee2015-08-18 14:49:01 -04002157
Martin Radev4c4c8e72016-08-04 12:25:34 +03002158bool Program::validateVertexAndFragmentInterfaceBlocks(
2159 const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks,
2160 const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks,
2161 InfoLog &infoLog) const
2162{
2163 // Check that interface blocks defined in the vertex and fragment shaders are identical
2164 typedef std::map<std::string, const sh::InterfaceBlock *> UniformBlockMap;
2165 UniformBlockMap linkedUniformBlocks;
2166
2167 for (const sh::InterfaceBlock &vertexInterfaceBlock : vertexInterfaceBlocks)
2168 {
2169 linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
2170 }
2171
Jamie Madille473dee2015-08-18 14:49:01 -04002172 for (const sh::InterfaceBlock &fragmentInterfaceBlock : fragmentInterfaceBlocks)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002173 {
Jamie Madille473dee2015-08-18 14:49:01 -04002174 auto entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002175 if (entry != linkedUniformBlocks.end())
2176 {
2177 const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
2178 if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
2179 {
2180 return false;
2181 }
2182 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002183 }
2184 return true;
2185}
Jamie Madille473dee2015-08-18 14:49:01 -04002186
Martin Radev4c4c8e72016-08-04 12:25:34 +03002187bool Program::linkUniformBlocks(InfoLog &infoLog, const Caps &caps)
2188{
2189 if (mState.mAttachedComputeShader)
2190 {
2191 const Shader &computeShader = *mState.mAttachedComputeShader;
2192 const auto &computeInterfaceBlocks = computeShader.getInterfaceBlocks();
2193
2194 if (!validateUniformBlocksCount(
2195 caps.maxComputeUniformBlocks, computeInterfaceBlocks,
2196 "Compute shader uniform block count exceeds GL_MAX_COMPUTE_UNIFORM_BLOCKS (",
2197 infoLog))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002198 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002199 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002200 }
Martin Radev4c4c8e72016-08-04 12:25:34 +03002201 return true;
2202 }
2203
2204 const Shader &vertexShader = *mState.mAttachedVertexShader;
2205 const Shader &fragmentShader = *mState.mAttachedFragmentShader;
2206
2207 const auto &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
2208 const auto &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
2209
2210 if (!validateUniformBlocksCount(
2211 caps.maxVertexUniformBlocks, vertexInterfaceBlocks,
2212 "Vertex shader uniform block count exceeds GL_MAX_VERTEX_UNIFORM_BLOCKS (", infoLog))
2213 {
2214 return false;
2215 }
2216 if (!validateUniformBlocksCount(
2217 caps.maxFragmentUniformBlocks, fragmentInterfaceBlocks,
2218 "Fragment shader uniform block count exceeds GL_MAX_FRAGMENT_UNIFORM_BLOCKS (",
2219 infoLog))
2220 {
2221
2222 return false;
2223 }
2224 if (!validateVertexAndFragmentInterfaceBlocks(vertexInterfaceBlocks, fragmentInterfaceBlocks,
2225 infoLog))
2226 {
2227 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002228 }
Jamie Madille473dee2015-08-18 14:49:01 -04002229
Geoff Lang7dd2e102014-11-10 15:19:26 -05002230 return true;
2231}
2232
Jamie Madilla2c74982016-12-12 11:20:42 -05002233bool Program::areMatchingInterfaceBlocks(InfoLog &infoLog,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002234 const sh::InterfaceBlock &vertexInterfaceBlock,
2235 const sh::InterfaceBlock &fragmentInterfaceBlock) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002236{
2237 const char* blockName = vertexInterfaceBlock.name.c_str();
2238 // validate blocks for the same member types
2239 if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
2240 {
Jamie Madillf6113162015-05-07 11:49:21 -04002241 infoLog << "Types for interface block '" << blockName
2242 << "' differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002243 return false;
2244 }
2245 if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
2246 {
Jamie Madillf6113162015-05-07 11:49:21 -04002247 infoLog << "Array sizes differ for interface block '" << blockName
2248 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002249 return false;
2250 }
2251 if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
2252 {
Jamie Madillf6113162015-05-07 11:49:21 -04002253 infoLog << "Layout qualifiers differ for interface block '" << blockName
2254 << "' between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002255 return false;
2256 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002257 const unsigned int numBlockMembers =
2258 static_cast<unsigned int>(vertexInterfaceBlock.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002259 for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
2260 {
2261 const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
2262 const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
2263 if (vertexMember.name != fragmentMember.name)
2264 {
Jamie Madillf6113162015-05-07 11:49:21 -04002265 infoLog << "Name mismatch for field " << blockMemberIndex
2266 << " of interface block '" << blockName
2267 << "': (in vertex: '" << vertexMember.name
2268 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002269 return false;
2270 }
2271 std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
2272 if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
2273 {
2274 return false;
2275 }
2276 }
2277 return true;
2278}
2279
2280bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
2281 const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
2282{
2283 if (vertexVariable.type != fragmentVariable.type)
2284 {
Jamie Madillf6113162015-05-07 11:49:21 -04002285 infoLog << "Types for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002286 return false;
2287 }
2288 if (vertexVariable.arraySize != fragmentVariable.arraySize)
2289 {
Jamie Madillf6113162015-05-07 11:49:21 -04002290 infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002291 return false;
2292 }
2293 if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
2294 {
Jamie Madillf6113162015-05-07 11:49:21 -04002295 infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002296 return false;
2297 }
2298
2299 if (vertexVariable.fields.size() != fragmentVariable.fields.size())
2300 {
Jamie Madillf6113162015-05-07 11:49:21 -04002301 infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002302 return false;
2303 }
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002304 const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size());
Geoff Lang7dd2e102014-11-10 15:19:26 -05002305 for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
2306 {
2307 const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
2308 const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
2309
2310 if (vertexMember.name != fragmentMember.name)
2311 {
Jamie Madillf6113162015-05-07 11:49:21 -04002312 infoLog << "Name mismatch for field '" << memberIndex
2313 << "' of " << variableName
2314 << ": (in vertex: '" << vertexMember.name
2315 << "', in fragment: '" << fragmentMember.name << "')";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002316 return false;
2317 }
2318
2319 const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
2320 vertexMember.name + "'";
2321
2322 if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
2323 {
2324 return false;
2325 }
2326 }
2327
2328 return true;
2329}
2330
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002331bool Program::linkValidateVaryings(InfoLog &infoLog,
2332 const std::string &varyingName,
2333 const sh::Varying &vertexVarying,
2334 const sh::Varying &fragmentVarying,
2335 int shaderVersion)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002336{
2337 if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
2338 {
2339 return false;
2340 }
2341
Jamie Madille9cc4692015-02-19 16:00:13 -05002342 if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation))
Geoff Lang7dd2e102014-11-10 15:19:26 -05002343 {
Yuly Novikova1f6dc92016-06-15 23:27:04 -04002344 infoLog << "Interpolation types for " << varyingName
2345 << " differ between vertex and fragment shaders.";
2346 return false;
2347 }
2348
2349 if (shaderVersion == 100 && vertexVarying.isInvariant != fragmentVarying.isInvariant)
2350 {
2351 infoLog << "Invariance for " << varyingName
2352 << " differs between vertex and fragment shaders.";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002353 return false;
2354 }
2355
2356 return true;
2357}
2358
Yuly Novikov817232e2017-02-22 18:36:10 -05002359bool Program::linkValidateBuiltInVaryings(InfoLog &infoLog) const
2360{
2361 const Shader *vertexShader = mState.mAttachedVertexShader;
2362 const Shader *fragmentShader = mState.mAttachedFragmentShader;
2363 const std::vector<sh::Varying> &vertexVaryings = vertexShader->getVaryings();
2364 const std::vector<sh::Varying> &fragmentVaryings = fragmentShader->getVaryings();
2365 int shaderVersion = vertexShader->getShaderVersion();
2366
2367 if (shaderVersion != 100)
2368 {
2369 // Only ESSL 1.0 has restrictions on matching input and output invariance
2370 return true;
2371 }
2372
2373 bool glPositionIsInvariant = false;
2374 bool glPointSizeIsInvariant = false;
2375 bool glFragCoordIsInvariant = false;
2376 bool glPointCoordIsInvariant = false;
2377
2378 for (const sh::Varying &varying : vertexVaryings)
2379 {
2380 if (!varying.isBuiltIn())
2381 {
2382 continue;
2383 }
2384 if (varying.name.compare("gl_Position") == 0)
2385 {
2386 glPositionIsInvariant = varying.isInvariant;
2387 }
2388 else if (varying.name.compare("gl_PointSize") == 0)
2389 {
2390 glPointSizeIsInvariant = varying.isInvariant;
2391 }
2392 }
2393
2394 for (const sh::Varying &varying : fragmentVaryings)
2395 {
2396 if (!varying.isBuiltIn())
2397 {
2398 continue;
2399 }
2400 if (varying.name.compare("gl_FragCoord") == 0)
2401 {
2402 glFragCoordIsInvariant = varying.isInvariant;
2403 }
2404 else if (varying.name.compare("gl_PointCoord") == 0)
2405 {
2406 glPointCoordIsInvariant = varying.isInvariant;
2407 }
2408 }
2409
2410 // There is some ambiguity in ESSL 1.00.17 paragraph 4.6.4 interpretation,
2411 // for example, https://cvs.khronos.org/bugzilla/show_bug.cgi?id=13842.
2412 // Not requiring invariance to match is supported by:
2413 // dEQP, WebGL CTS, Nexus 5X GLES
2414 if (glFragCoordIsInvariant && !glPositionIsInvariant)
2415 {
2416 infoLog << "gl_FragCoord can only be declared invariant if and only if gl_Position is "
2417 "declared invariant.";
2418 return false;
2419 }
2420 if (glPointCoordIsInvariant && !glPointSizeIsInvariant)
2421 {
2422 infoLog << "gl_PointCoord can only be declared invariant if and only if gl_PointSize is "
2423 "declared invariant.";
2424 return false;
2425 }
2426
2427 return true;
2428}
2429
Jamie Madillccdf74b2015-08-18 10:46:12 -04002430bool Program::linkValidateTransformFeedback(InfoLog &infoLog,
Jamie Madill192745a2016-12-22 15:58:21 -05002431 const Program::MergedVaryings &varyings,
Jamie Madillccdf74b2015-08-18 10:46:12 -04002432 const Caps &caps) const
Geoff Lang7dd2e102014-11-10 15:19:26 -05002433{
2434 size_t totalComponents = 0;
2435
Jamie Madillccdf74b2015-08-18 10:46:12 -04002436 std::set<std::string> uniqueNames;
2437
Jamie Madill48ef11b2016-04-27 15:21:52 -04002438 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002439 {
2440 bool found = false;
Jamie Madill192745a2016-12-22 15:58:21 -05002441 for (const auto &ref : varyings)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002442 {
Jamie Madill192745a2016-12-22 15:58:21 -05002443 const sh::Varying *varying = ref.second.get();
2444
Jamie Madillccdf74b2015-08-18 10:46:12 -04002445 if (tfVaryingName == varying->name)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002446 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002447 if (uniqueNames.count(tfVaryingName) > 0)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002448 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002449 infoLog << "Two transform feedback varyings specify the same output variable ("
2450 << tfVaryingName << ").";
2451 return false;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002452 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002453 uniqueNames.insert(tfVaryingName);
Geoff Lang7dd2e102014-11-10 15:19:26 -05002454
Geoff Lang1a683462015-09-29 15:09:59 -04002455 if (varying->isArray())
2456 {
2457 infoLog << "Capture of arrays is undefined and not supported.";
2458 return false;
2459 }
2460
Jamie Madillccdf74b2015-08-18 10:46:12 -04002461 // TODO(jmadill): Investigate implementation limits on D3D11
Jamie Madilla2c74982016-12-12 11:20:42 -05002462 size_t componentCount = VariableComponentCount(varying->type);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002463 if (mState.mTransformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
Geoff Lang7dd2e102014-11-10 15:19:26 -05002464 componentCount > caps.maxTransformFeedbackSeparateComponents)
2465 {
Jamie Madillccdf74b2015-08-18 10:46:12 -04002466 infoLog << "Transform feedback varying's " << varying->name << " components ("
2467 << componentCount << ") exceed the maximum separate components ("
Jamie Madillf6113162015-05-07 11:49:21 -04002468 << caps.maxTransformFeedbackSeparateComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002469 return false;
2470 }
2471
2472 totalComponents += componentCount;
Geoff Lang7dd2e102014-11-10 15:19:26 -05002473 found = true;
2474 break;
2475 }
2476 }
2477
Jamie Madill89bb70e2015-08-31 14:18:39 -04002478 if (tfVaryingName.find('[') != std::string::npos)
2479 {
Geoff Lang1a683462015-09-29 15:09:59 -04002480 infoLog << "Capture of array elements is undefined and not supported.";
Jamie Madill89bb70e2015-08-31 14:18:39 -04002481 return false;
2482 }
2483
Geoff Lang7dd2e102014-11-10 15:19:26 -05002484 // All transform feedback varyings are expected to exist since packVaryings checks for them.
2485 ASSERT(found);
2486 }
2487
Jamie Madill48ef11b2016-04-27 15:21:52 -04002488 if (mState.mTransformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS &&
Jamie Madillf6113162015-05-07 11:49:21 -04002489 totalComponents > caps.maxTransformFeedbackInterleavedComponents)
Geoff Lang7dd2e102014-11-10 15:19:26 -05002490 {
Jamie Madillf6113162015-05-07 11:49:21 -04002491 infoLog << "Transform feedback varying total components (" << totalComponents
2492 << ") exceed the maximum interleaved components ("
2493 << caps.maxTransformFeedbackInterleavedComponents << ").";
Geoff Lang7dd2e102014-11-10 15:19:26 -05002494 return false;
2495 }
2496
2497 return true;
Geoff Lang1b6edcb2014-02-03 14:27:56 -05002498}
2499
Jamie Madill192745a2016-12-22 15:58:21 -05002500void Program::gatherTransformFeedbackVaryings(const Program::MergedVaryings &varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002501{
2502 // Gather the linked varyings that are used for transform feedback, they should all exist.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002503 mState.mTransformFeedbackVaryingVars.clear();
2504 for (const std::string &tfVaryingName : mState.mTransformFeedbackVaryingNames)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002505 {
Jamie Madill192745a2016-12-22 15:58:21 -05002506 for (const auto &ref : varyings)
Jamie Madillccdf74b2015-08-18 10:46:12 -04002507 {
Jamie Madill192745a2016-12-22 15:58:21 -05002508 const sh::Varying *varying = ref.second.get();
Jamie Madillccdf74b2015-08-18 10:46:12 -04002509 if (tfVaryingName == varying->name)
2510 {
Jamie Madill48ef11b2016-04-27 15:21:52 -04002511 mState.mTransformFeedbackVaryingVars.push_back(*varying);
Jamie Madillccdf74b2015-08-18 10:46:12 -04002512 break;
2513 }
2514 }
2515 }
2516}
2517
Jamie Madill192745a2016-12-22 15:58:21 -05002518Program::MergedVaryings Program::getMergedVaryings() const
Jamie Madillccdf74b2015-08-18 10:46:12 -04002519{
Jamie Madill192745a2016-12-22 15:58:21 -05002520 MergedVaryings merged;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002521
Jamie Madill48ef11b2016-04-27 15:21:52 -04002522 for (const sh::Varying &varying : mState.mAttachedVertexShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002523 {
Jamie Madill192745a2016-12-22 15:58:21 -05002524 merged[varying.name].vertex = &varying;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002525 }
2526
Jamie Madill48ef11b2016-04-27 15:21:52 -04002527 for (const sh::Varying &varying : mState.mAttachedFragmentShader->getVaryings())
Jamie Madillccdf74b2015-08-18 10:46:12 -04002528 {
Jamie Madill192745a2016-12-22 15:58:21 -05002529 merged[varying.name].fragment = &varying;
2530 }
2531
2532 return merged;
2533}
2534
2535std::vector<PackedVarying> Program::getPackedVaryings(
2536 const Program::MergedVaryings &mergedVaryings) const
2537{
2538 const std::vector<std::string> &tfVaryings = mState.getTransformFeedbackVaryingNames();
2539 std::vector<PackedVarying> packedVaryings;
2540
2541 for (const auto &ref : mergedVaryings)
2542 {
2543 const sh::Varying *input = ref.second.vertex;
2544 const sh::Varying *output = ref.second.fragment;
2545
2546 // Only pack varyings that have a matched input or output, plus special builtins.
2547 if ((input && output) || (output && output->isBuiltIn()))
Jamie Madillccdf74b2015-08-18 10:46:12 -04002548 {
Jamie Madill192745a2016-12-22 15:58:21 -05002549 // Will get the vertex shader interpolation by default.
2550 auto interpolation = ref.second.get()->interpolation;
2551
2552 // Interpolation qualifiers must match.
2553 if (output->isStruct())
2554 {
2555 ASSERT(!output->isArray());
2556 for (const auto &field : output->fields)
2557 {
2558 ASSERT(!field.isStruct() && !field.isArray());
2559 packedVaryings.push_back(PackedVarying(field, interpolation, output->name));
2560 }
2561 }
2562 else
2563 {
2564 packedVaryings.push_back(PackedVarying(*output, interpolation));
2565 }
2566 continue;
2567 }
2568
2569 // Keep Transform FB varyings in the merged list always.
2570 if (!input)
2571 {
2572 continue;
2573 }
2574
2575 for (const std::string &tfVarying : tfVaryings)
2576 {
2577 if (tfVarying == input->name)
2578 {
2579 // Transform feedback for varying structs is underspecified.
2580 // See Khronos bug 9856.
2581 // TODO(jmadill): Figure out how to be spec-compliant here.
2582 if (!input->isStruct())
2583 {
2584 packedVaryings.push_back(PackedVarying(*input, input->interpolation));
2585 packedVaryings.back().vertexOnly = true;
2586 }
2587 break;
2588 }
Jamie Madillccdf74b2015-08-18 10:46:12 -04002589 }
2590 }
2591
Jamie Madill192745a2016-12-22 15:58:21 -05002592 std::sort(packedVaryings.begin(), packedVaryings.end(), ComparePackedVarying);
2593
2594 return packedVaryings;
Jamie Madillccdf74b2015-08-18 10:46:12 -04002595}
Jamie Madill80a6fc02015-08-21 16:53:16 -04002596
2597void Program::linkOutputVariables()
2598{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002599 const Shader *fragmentShader = mState.mAttachedFragmentShader;
Jamie Madill80a6fc02015-08-21 16:53:16 -04002600 ASSERT(fragmentShader != nullptr);
2601
2602 // Skip this step for GLES2 shaders.
2603 if (fragmentShader->getShaderVersion() == 100)
2604 return;
2605
jchen1015015f72017-03-16 13:54:21 +08002606 mState.mOutputVariables = fragmentShader->getActiveOutputVariables();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002607 // TODO(jmadill): any caps validation here?
2608
jchen1015015f72017-03-16 13:54:21 +08002609 for (unsigned int outputVariableIndex = 0; outputVariableIndex < mState.mOutputVariables.size();
Jamie Madill80a6fc02015-08-21 16:53:16 -04002610 outputVariableIndex++)
2611 {
jchen1015015f72017-03-16 13:54:21 +08002612 const sh::OutputVariable &outputVariable = mState.mOutputVariables[outputVariableIndex];
Jamie Madill80a6fc02015-08-21 16:53:16 -04002613
2614 // Don't store outputs for gl_FragDepth, gl_FragColor, etc.
2615 if (outputVariable.isBuiltIn())
2616 continue;
2617
2618 // Since multiple output locations must be specified, use 0 for non-specified locations.
2619 int baseLocation = (outputVariable.location == -1 ? 0 : outputVariable.location);
2620
Jamie Madill80a6fc02015-08-21 16:53:16 -04002621 for (unsigned int elementIndex = 0; elementIndex < outputVariable.elementCount();
2622 elementIndex++)
2623 {
2624 const int location = baseLocation + elementIndex;
jchen1015015f72017-03-16 13:54:21 +08002625 ASSERT(mState.mOutputLocations.count(location) == 0);
Jamie Madill80a6fc02015-08-21 16:53:16 -04002626 unsigned int element = outputVariable.isArray() ? elementIndex : GL_INVALID_INDEX;
jchen1015015f72017-03-16 13:54:21 +08002627 mState.mOutputLocations[location] =
Jamie Madill80a6fc02015-08-21 16:53:16 -04002628 VariableLocation(outputVariable.name, element, outputVariableIndex);
2629 }
2630 }
2631}
Jamie Madill62d31cb2015-09-11 13:25:51 -04002632
Olli Etuaho48fed632017-03-16 12:05:30 +00002633void Program::setUniformValuesFromBindingQualifiers()
2634{
2635 for (unsigned int samplerIndex = mState.mSamplerUniformRange.start;
2636 samplerIndex < mState.mSamplerUniformRange.end; ++samplerIndex)
2637 {
2638 const auto &samplerUniform = mState.mUniforms[samplerIndex];
2639 if (samplerUniform.binding != -1)
2640 {
2641 GLint location = mState.getUniformLocation(samplerUniform.name);
2642 ASSERT(location != -1);
2643 std::vector<GLint> boundTextureUnits;
2644 for (unsigned int elementIndex = 0; elementIndex < samplerUniform.elementCount();
2645 ++elementIndex)
2646 {
2647 boundTextureUnits.push_back(samplerUniform.binding + elementIndex);
2648 }
2649 setUniform1iv(location, static_cast<GLsizei>(boundTextureUnits.size()),
2650 boundTextureUnits.data());
2651 }
2652 }
2653}
2654
Jamie Madill62d31cb2015-09-11 13:25:51 -04002655void Program::gatherInterfaceBlockInfo()
2656{
Martin Radev4c4c8e72016-08-04 12:25:34 +03002657 ASSERT(mState.mUniformBlocks.empty());
2658
2659 if (mState.mAttachedComputeShader)
2660 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002661 const Shader *computeShader = mState.getAttachedComputeShader();
Martin Radev4c4c8e72016-08-04 12:25:34 +03002662
2663 for (const sh::InterfaceBlock &computeBlock : computeShader->getInterfaceBlocks())
2664 {
2665
2666 // Only 'packed' blocks are allowed to be considered inactive.
2667 if (!computeBlock.staticUse && computeBlock.layout == sh::BLOCKLAYOUT_PACKED)
2668 continue;
2669
Jamie Madilla2c74982016-12-12 11:20:42 -05002670 for (UniformBlock &block : mState.mUniformBlocks)
Martin Radev4c4c8e72016-08-04 12:25:34 +03002671 {
2672 if (block.name == computeBlock.name)
2673 {
2674 block.computeStaticUse = computeBlock.staticUse;
2675 }
2676 }
2677
2678 defineUniformBlock(computeBlock, GL_COMPUTE_SHADER);
2679 }
2680 return;
2681 }
2682
Jamie Madill62d31cb2015-09-11 13:25:51 -04002683 std::set<std::string> visitedList;
2684
Jamie Madilla2c74982016-12-12 11:20:42 -05002685 const Shader *vertexShader = mState.getAttachedVertexShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002686
Jamie Madill62d31cb2015-09-11 13:25:51 -04002687 for (const sh::InterfaceBlock &vertexBlock : vertexShader->getInterfaceBlocks())
2688 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002689 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002690 if (!vertexBlock.staticUse && vertexBlock.layout == sh::BLOCKLAYOUT_PACKED)
2691 continue;
2692
2693 if (visitedList.count(vertexBlock.name) > 0)
2694 continue;
2695
2696 defineUniformBlock(vertexBlock, GL_VERTEX_SHADER);
2697 visitedList.insert(vertexBlock.name);
2698 }
2699
Jamie Madilla2c74982016-12-12 11:20:42 -05002700 const Shader *fragmentShader = mState.getAttachedFragmentShader();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002701
2702 for (const sh::InterfaceBlock &fragmentBlock : fragmentShader->getInterfaceBlocks())
2703 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002704 // Only 'packed' blocks are allowed to be considered inactive.
Jamie Madill62d31cb2015-09-11 13:25:51 -04002705 if (!fragmentBlock.staticUse && fragmentBlock.layout == sh::BLOCKLAYOUT_PACKED)
2706 continue;
2707
2708 if (visitedList.count(fragmentBlock.name) > 0)
2709 {
Jamie Madilla2c74982016-12-12 11:20:42 -05002710 for (UniformBlock &block : mState.mUniformBlocks)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002711 {
2712 if (block.name == fragmentBlock.name)
2713 {
2714 block.fragmentStaticUse = fragmentBlock.staticUse;
2715 }
2716 }
2717
2718 continue;
2719 }
2720
2721 defineUniformBlock(fragmentBlock, GL_FRAGMENT_SHADER);
2722 visitedList.insert(fragmentBlock.name);
2723 }
2724}
2725
Jamie Madill4a3c2342015-10-08 12:58:45 -04002726template <typename VarT>
2727void Program::defineUniformBlockMembers(const std::vector<VarT> &fields,
2728 const std::string &prefix,
2729 int blockIndex)
2730{
2731 for (const VarT &field : fields)
2732 {
2733 const std::string &fullName = (prefix.empty() ? field.name : prefix + "." + field.name);
2734
2735 if (field.isStruct())
2736 {
2737 for (unsigned int arrayElement = 0; arrayElement < field.elementCount(); arrayElement++)
2738 {
2739 const std::string uniformElementName =
2740 fullName + (field.isArray() ? ArrayString(arrayElement) : "");
2741 defineUniformBlockMembers(field.fields, uniformElementName, blockIndex);
2742 }
2743 }
2744 else
2745 {
2746 // If getBlockMemberInfo returns false, the uniform is optimized out.
2747 sh::BlockMemberInfo memberInfo;
2748 if (!mProgram->getUniformBlockMemberInfo(fullName, &memberInfo))
2749 {
2750 continue;
2751 }
2752
Olli Etuaho6ca2b652017-02-19 18:05:10 +00002753 LinkedUniform newUniform(field.type, field.precision, fullName, field.arraySize, -1, -1,
Jamie Madill4a3c2342015-10-08 12:58:45 -04002754 blockIndex, memberInfo);
2755
2756 // Since block uniforms have no location, we don't need to store them in the uniform
2757 // locations list.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002758 mState.mUniforms.push_back(newUniform);
Jamie Madill4a3c2342015-10-08 12:58:45 -04002759 }
2760 }
2761}
2762
Jamie Madill62d31cb2015-09-11 13:25:51 -04002763void Program::defineUniformBlock(const sh::InterfaceBlock &interfaceBlock, GLenum shaderType)
2764{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002765 int blockIndex = static_cast<int>(mState.mUniformBlocks.size());
Jamie Madill4a3c2342015-10-08 12:58:45 -04002766 size_t blockSize = 0;
2767
2768 // Don't define this block at all if it's not active in the implementation.
Qin Jiajia0350a642016-11-01 17:01:51 +08002769 std::stringstream blockNameStr;
2770 blockNameStr << interfaceBlock.name;
2771 if (interfaceBlock.arraySize > 0)
2772 {
2773 blockNameStr << "[0]";
2774 }
2775 if (!mProgram->getUniformBlockSize(blockNameStr.str(), &blockSize))
Jamie Madill4a3c2342015-10-08 12:58:45 -04002776 {
2777 return;
2778 }
2779
2780 // Track the first and last uniform index to determine the range of active uniforms in the
2781 // block.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002782 size_t firstBlockUniformIndex = mState.mUniforms.size();
Jamie Madill39046162016-02-08 15:05:17 -05002783 defineUniformBlockMembers(interfaceBlock.fields, interfaceBlock.fieldPrefix(), blockIndex);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002784 size_t lastBlockUniformIndex = mState.mUniforms.size();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002785
2786 std::vector<unsigned int> blockUniformIndexes;
2787 for (size_t blockUniformIndex = firstBlockUniformIndex;
2788 blockUniformIndex < lastBlockUniformIndex; ++blockUniformIndex)
2789 {
2790 blockUniformIndexes.push_back(static_cast<unsigned int>(blockUniformIndex));
2791 }
2792
2793 if (interfaceBlock.arraySize > 0)
2794 {
2795 for (unsigned int arrayElement = 0; arrayElement < interfaceBlock.arraySize; ++arrayElement)
2796 {
2797 UniformBlock block(interfaceBlock.name, true, arrayElement);
2798 block.memberUniformIndexes = blockUniformIndexes;
2799
Martin Radev4c4c8e72016-08-04 12:25:34 +03002800 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002801 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002802 case GL_VERTEX_SHADER:
2803 {
2804 block.vertexStaticUse = interfaceBlock.staticUse;
2805 break;
2806 }
2807 case GL_FRAGMENT_SHADER:
2808 {
2809 block.fragmentStaticUse = interfaceBlock.staticUse;
2810 break;
2811 }
2812 case GL_COMPUTE_SHADER:
2813 {
2814 block.computeStaticUse = interfaceBlock.staticUse;
2815 break;
2816 }
2817 default:
2818 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002819 }
2820
Qin Jiajia0350a642016-11-01 17:01:51 +08002821 // Since all block elements in an array share the same active uniforms, they will all be
2822 // active once any uniform member is used. So, since interfaceBlock.name[0] was active,
2823 // here we will add every block element in the array.
2824 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002825 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002826 }
2827 }
2828 else
2829 {
2830 UniformBlock block(interfaceBlock.name, false, 0);
2831 block.memberUniformIndexes = blockUniformIndexes;
2832
Martin Radev4c4c8e72016-08-04 12:25:34 +03002833 switch (shaderType)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002834 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002835 case GL_VERTEX_SHADER:
2836 {
2837 block.vertexStaticUse = interfaceBlock.staticUse;
2838 break;
2839 }
2840 case GL_FRAGMENT_SHADER:
2841 {
2842 block.fragmentStaticUse = interfaceBlock.staticUse;
2843 break;
2844 }
2845 case GL_COMPUTE_SHADER:
2846 {
2847 block.computeStaticUse = interfaceBlock.staticUse;
2848 break;
2849 }
2850 default:
2851 UNREACHABLE();
Jamie Madill62d31cb2015-09-11 13:25:51 -04002852 }
2853
Jamie Madill4a3c2342015-10-08 12:58:45 -04002854 block.dataSize = static_cast<unsigned int>(blockSize);
Jamie Madill48ef11b2016-04-27 15:21:52 -04002855 mState.mUniformBlocks.push_back(block);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002856 }
2857}
2858
Jamie Madille7d84322017-01-10 18:21:59 -05002859template <>
2860void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2861 const uint8_t *destPointer,
2862 GLsizei clampedCount,
2863 const GLint *v)
2864{
2865 // Invalidate the validation cache only if we modify the sampler data.
2866 if (mState.isSamplerUniformIndex(locationInfo.index) &&
2867 memcmp(destPointer, v, sizeof(GLint) * clampedCount) != 0)
2868 {
2869 GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationInfo.index);
2870 std::vector<GLuint> *boundTextureUnits =
2871 &mState.mSamplerBindings[samplerIndex].boundTextureUnits;
2872
2873 std::copy(v, v + clampedCount, boundTextureUnits->begin() + locationInfo.element);
2874 mCachedValidateSamplersResult.reset();
2875 }
2876}
2877
2878template <typename T>
2879void Program::updateSamplerUniform(const VariableLocation &locationInfo,
2880 const uint8_t *destPointer,
2881 GLsizei clampedCount,
2882 const T *v)
2883{
2884}
2885
Jamie Madill62d31cb2015-09-11 13:25:51 -04002886template <typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002887GLsizei Program::setUniformInternal(GLint location, GLsizei countIn, int vectorSize, const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002888{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002889 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2890 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002891 uint8_t *destPointer = linkedUniform->getDataPtrToElement(locationInfo.element);
2892
Corentin Wallez15ac5342016-11-03 17:06:39 -04002893 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2894 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2895 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002896 GLsizei maxElementCount =
2897 static_cast<GLsizei>(remainingElements * linkedUniform->getElementComponents());
2898
2899 GLsizei count = countIn;
2900 GLsizei clampedCount = count * vectorSize;
2901 if (clampedCount > maxElementCount)
2902 {
2903 clampedCount = maxElementCount;
2904 count = maxElementCount / vectorSize;
2905 }
Corentin Wallez15ac5342016-11-03 17:06:39 -04002906
Jamie Madill62d31cb2015-09-11 13:25:51 -04002907 if (VariableComponentType(linkedUniform->type) == GL_BOOL)
2908 {
2909 // Do a cast conversion for boolean types. From the spec:
2910 // "The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise."
2911 GLint *destAsInt = reinterpret_cast<GLint *>(destPointer);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002912 for (GLsizei component = 0; component < clampedCount; ++component)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002913 {
2914 destAsInt[component] = (v[component] != static_cast<T>(0) ? GL_TRUE : GL_FALSE);
2915 }
2916 }
2917 else
2918 {
Jamie Madille7d84322017-01-10 18:21:59 -05002919 updateSamplerUniform(locationInfo, destPointer, clampedCount, v);
Corentin Wallez15ac5342016-11-03 17:06:39 -04002920 memcpy(destPointer, v, sizeof(T) * clampedCount);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002921 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002922
2923 return count;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002924}
2925
2926template <size_t cols, size_t rows, typename T>
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002927GLsizei Program::setMatrixUniformInternal(GLint location,
2928 GLsizei count,
2929 GLboolean transpose,
2930 const T *v)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002931{
2932 if (!transpose)
2933 {
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002934 return setUniformInternal(location, count, cols * rows, v);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002935 }
2936
2937 // Perform a transposing copy.
Jamie Madill48ef11b2016-04-27 15:21:52 -04002938 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2939 LinkedUniform *linkedUniform = &mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002940 T *destPtr = reinterpret_cast<T *>(linkedUniform->getDataPtrToElement(locationInfo.element));
Corentin Wallez15ac5342016-11-03 17:06:39 -04002941
2942 // OpenGL ES 3.0.4 spec pg 67: "Values for any array element that exceeds the highest array
2943 // element index used, as reported by GetActiveUniform, will be ignored by the GL."
2944 unsigned int remainingElements = linkedUniform->elementCount() - locationInfo.element;
2945 GLsizei clampedCount = std::min(count, static_cast<GLsizei>(remainingElements));
2946
2947 for (GLsizei element = 0; element < clampedCount; ++element)
Jamie Madill62d31cb2015-09-11 13:25:51 -04002948 {
2949 size_t elementOffset = element * rows * cols;
2950
2951 for (size_t row = 0; row < rows; ++row)
2952 {
2953 for (size_t col = 0; col < cols; ++col)
2954 {
2955 destPtr[col * rows + row + elementOffset] = v[row * cols + col + elementOffset];
2956 }
2957 }
2958 }
Corentin Wallez8b7d8142016-11-15 13:40:37 -05002959
2960 return clampedCount;
Jamie Madill62d31cb2015-09-11 13:25:51 -04002961}
2962
2963template <typename DestT>
2964void Program::getUniformInternal(GLint location, DestT *dataOut) const
2965{
Jamie Madill48ef11b2016-04-27 15:21:52 -04002966 const VariableLocation &locationInfo = mState.mUniformLocations[location];
2967 const LinkedUniform &uniform = mState.mUniforms[locationInfo.index];
Jamie Madill62d31cb2015-09-11 13:25:51 -04002968
2969 const uint8_t *srcPointer = uniform.getDataPtrToElement(locationInfo.element);
2970
2971 GLenum componentType = VariableComponentType(uniform.type);
2972 if (componentType == GLTypeToGLenum<DestT>::value)
2973 {
2974 memcpy(dataOut, srcPointer, uniform.getElementSize());
2975 return;
2976 }
2977
Corentin Wallez6596c462016-03-17 17:26:58 -04002978 int components = VariableComponentCount(uniform.type);
Jamie Madill62d31cb2015-09-11 13:25:51 -04002979
2980 switch (componentType)
2981 {
2982 case GL_INT:
2983 UniformStateQueryCastLoop<GLint>(dataOut, srcPointer, components);
2984 break;
2985 case GL_UNSIGNED_INT:
2986 UniformStateQueryCastLoop<GLuint>(dataOut, srcPointer, components);
2987 break;
2988 case GL_BOOL:
2989 UniformStateQueryCastLoop<GLboolean>(dataOut, srcPointer, components);
2990 break;
2991 case GL_FLOAT:
2992 UniformStateQueryCastLoop<GLfloat>(dataOut, srcPointer, components);
2993 break;
2994 default:
2995 UNREACHABLE();
2996 }
2997}
Jamie Madilla4595b82017-01-11 17:36:34 -05002998
2999bool Program::samplesFromTexture(const gl::State &state, GLuint textureID) const
3000{
3001 // Must be called after samplers are validated.
3002 ASSERT(mCachedValidateSamplersResult.valid() && mCachedValidateSamplersResult.value());
3003
3004 for (const auto &binding : mState.mSamplerBindings)
3005 {
3006 GLenum textureType = binding.textureType;
3007 for (const auto &unit : binding.boundTextureUnits)
3008 {
3009 GLenum programTextureID = state.getSamplerTextureId(unit, textureType);
3010 if (programTextureID == textureID)
3011 {
3012 // TODO(jmadill): Check for appropriate overlap.
3013 return true;
3014 }
3015 }
3016 }
3017
3018 return false;
3019}
3020
Jamie Madilla2c74982016-12-12 11:20:42 -05003021} // namespace gl