blob: 84a4e3d536e7c00b595bae1fc1bd24d91b102e88 [file] [log] [blame]
Jamie Madill6a729792014-07-18 10:33:14 -04001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
Jamie Madilla2ad4e82014-07-17 14:16:32 -04006// ShaderVars.cpp:
Jamie Madill6a729792014-07-18 10:33:14 -04007// Methods for GL variable types (varyings, uniforms, etc)
8//
9
Jamie Madille294bb82014-07-17 14:16:26 -040010#include <GLSLANG/ShaderLang.h>
Jamie Madill6a729792014-07-18 10:33:14 -040011
Olli Etuahod57e0db2015-04-24 15:05:08 +030012#include "common/debug.h"
Zhenyao Moed136362014-10-03 13:23:01 -070013
Jamie Madill6a729792014-07-18 10:33:14 -040014namespace sh
15{
16
Jamie Madille9cc4692015-02-19 16:00:13 -050017namespace
18{
19
20InterpolationType GetNonAuxiliaryInterpolationType(InterpolationType interpolation)
21{
22 return (interpolation == INTERPOLATION_CENTROID ? INTERPOLATION_SMOOTH : interpolation);
23}
Jamie Madille9cc4692015-02-19 16:00:13 -050024}
25// The ES 3.0 spec is not clear on this point, but the ES 3.1 spec, and discussion
26// on Khronos.org, clarifies that a smooth/flat mismatch produces a link error,
27// but auxiliary qualifier mismatch (centroid) does not.
28bool InterpolationTypesMatch(InterpolationType a, InterpolationType b)
29{
30 return (GetNonAuxiliaryInterpolationType(a) == GetNonAuxiliaryInterpolationType(b));
31}
32
Jamie Madilld7b1ab52016-12-12 14:42:19 -050033ShaderVariable::ShaderVariable() : type(0), precision(0), arraySize(0), staticUse(false)
34{
35}
Jamie Madill6a729792014-07-18 10:33:14 -040036
37ShaderVariable::ShaderVariable(GLenum typeIn, unsigned int arraySizeIn)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050038 : type(typeIn), precision(0), arraySize(arraySizeIn), staticUse(false)
39{
40}
Jamie Madill6a729792014-07-18 10:33:14 -040041
42ShaderVariable::~ShaderVariable()
Jamie Madilld7b1ab52016-12-12 14:42:19 -050043{
44}
Jamie Madill6a729792014-07-18 10:33:14 -040045
46ShaderVariable::ShaderVariable(const ShaderVariable &other)
47 : type(other.type),
48 precision(other.precision),
49 name(other.name),
50 mappedName(other.mappedName),
51 arraySize(other.arraySize),
Jamie Madill42bcf322014-08-25 16:20:46 -040052 staticUse(other.staticUse),
53 fields(other.fields),
54 structName(other.structName)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050055{
56}
Jamie Madill6a729792014-07-18 10:33:14 -040057
58ShaderVariable &ShaderVariable::operator=(const ShaderVariable &other)
59{
Jamie Madilld7b1ab52016-12-12 14:42:19 -050060 type = other.type;
61 precision = other.precision;
62 name = other.name;
Jamie Madill6a729792014-07-18 10:33:14 -040063 mappedName = other.mappedName;
Jamie Madilld7b1ab52016-12-12 14:42:19 -050064 arraySize = other.arraySize;
65 staticUse = other.staticUse;
66 fields = other.fields;
Jamie Madill42bcf322014-08-25 16:20:46 -040067 structName = other.structName;
Jamie Madill6a729792014-07-18 10:33:14 -040068 return *this;
69}
70
Zhenyao Moed136362014-10-03 13:23:01 -070071bool ShaderVariable::operator==(const ShaderVariable &other) const
72{
Jamie Madilld7b1ab52016-12-12 14:42:19 -050073 if (type != other.type || precision != other.precision || name != other.name ||
74 mappedName != other.mappedName || arraySize != other.arraySize ||
75 staticUse != other.staticUse || fields.size() != other.fields.size() ||
Zhenyao Moed136362014-10-03 13:23:01 -070076 structName != other.structName)
77 {
78 return false;
79 }
80 for (size_t ii = 0; ii < fields.size(); ++ii)
81 {
82 if (fields[ii] != other.fields[ii])
83 return false;
84 }
85 return true;
86}
87
Jamie Madilld7b1ab52016-12-12 14:42:19 -050088bool ShaderVariable::findInfoByMappedName(const std::string &mappedFullName,
89 const ShaderVariable **leafVar,
90 std::string *originalFullName) const
Zhenyao Moed136362014-10-03 13:23:01 -070091{
92 ASSERT(leafVar && originalFullName);
93 // There are three cases:
94 // 1) the top variable is of struct type;
95 // 2) the top variable is an array;
96 // 3) otherwise.
97 size_t pos = mappedFullName.find_first_of(".[");
Zhenyao Moed136362014-10-03 13:23:01 -070098
99 if (pos == std::string::npos)
100 {
101 // Case 3.
102 if (mappedFullName != this->mappedName)
103 return false;
104 *originalFullName = this->name;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500105 *leafVar = this;
Zhenyao Moed136362014-10-03 13:23:01 -0700106 return true;
107 }
108 else
109 {
110 std::string topName = mappedFullName.substr(0, pos);
111 if (topName != this->mappedName)
112 return false;
113 std::string originalName = this->name;
114 std::string remaining;
115 if (mappedFullName[pos] == '[')
116 {
117 // Case 2.
118 size_t closePos = mappedFullName.find_first_of(']');
119 if (closePos < pos || closePos == std::string::npos)
120 return false;
121 // Append '[index]'.
122 originalName += mappedFullName.substr(pos, closePos - pos + 1);
123 if (closePos + 1 == mappedFullName.size())
124 {
125 *originalFullName = originalName;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500126 *leafVar = this;
Zhenyao Moed136362014-10-03 13:23:01 -0700127 return true;
128 }
129 else
130 {
131 // In the form of 'a[0].b', so after ']', '.' is expected.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500132 if (mappedFullName[closePos + 1] != '.')
Zhenyao Moed136362014-10-03 13:23:01 -0700133 return false;
134 remaining = mappedFullName.substr(closePos + 2); // Skip "]."
135 }
136 }
137 else
138 {
139 // Case 1.
140 remaining = mappedFullName.substr(pos + 1); // Skip "."
141 }
142 for (size_t ii = 0; ii < this->fields.size(); ++ii)
143 {
144 const ShaderVariable *fieldVar = NULL;
145 std::string originalFieldName;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500146 bool found = fields[ii].findInfoByMappedName(remaining, &fieldVar, &originalFieldName);
Zhenyao Moed136362014-10-03 13:23:01 -0700147 if (found)
148 {
149 *originalFullName = originalName + "." + originalFieldName;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500150 *leafVar = fieldVar;
Zhenyao Moed136362014-10-03 13:23:01 -0700151 return true;
152 }
153 }
154 return false;
155 }
156}
157
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500158bool ShaderVariable::isSameVariableAtLinkTime(const ShaderVariable &other,
159 bool matchPrecision) const
Zhenyao Moed136362014-10-03 13:23:01 -0700160{
161 if (type != other.type)
162 return false;
163 if (matchPrecision && precision != other.precision)
164 return false;
165 if (name != other.name)
166 return false;
167 ASSERT(mappedName == other.mappedName);
168 if (arraySize != other.arraySize)
169 return false;
170 if (fields.size() != other.fields.size())
171 return false;
172 for (size_t ii = 0; ii < fields.size(); ++ii)
173 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500174 if (!fields[ii].isSameVariableAtLinkTime(other.fields[ii], matchPrecision))
Zhenyao Moed136362014-10-03 13:23:01 -0700175 {
176 return false;
177 }
178 }
179 if (structName != other.structName)
180 return false;
181 return true;
182}
183
Olli Etuaho547cbd42017-02-27 11:54:00 +0200184Uniform::Uniform() : binding(-1)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500185{
186}
Jamie Madill6a729792014-07-18 10:33:14 -0400187
188Uniform::~Uniform()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500189{
190}
Jamie Madill6a729792014-07-18 10:33:14 -0400191
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000192Uniform::Uniform(const Uniform &other) : VariableWithLocation(other), binding(other.binding)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500193{
194}
Jamie Madill6a729792014-07-18 10:33:14 -0400195
196Uniform &Uniform::operator=(const Uniform &other)
197{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000198 VariableWithLocation::operator=(other);
Olli Etuaho547cbd42017-02-27 11:54:00 +0200199 binding = other.binding;
Jamie Madill6a729792014-07-18 10:33:14 -0400200 return *this;
201}
202
Zhenyao Moed136362014-10-03 13:23:01 -0700203bool Uniform::operator==(const Uniform &other) const
204{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000205 return VariableWithLocation::operator==(other) && binding == other.binding;
Zhenyao Moed136362014-10-03 13:23:01 -0700206}
207
208bool Uniform::isSameUniformAtLinkTime(const Uniform &other) const
209{
Olli Etuaho547cbd42017-02-27 11:54:00 +0200210 if (binding != -1 && other.binding != -1 && binding != other.binding)
211 {
212 return false;
213 }
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000214 if (location != -1 && other.location != -1 && location != other.location)
215 {
216 return false;
217 }
218 return VariableWithLocation::isSameVariableAtLinkTime(other, true);
Zhenyao Moed136362014-10-03 13:23:01 -0700219}
220
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000221VariableWithLocation::VariableWithLocation() : location(-1)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500222{
223}
Jamie Madill6a729792014-07-18 10:33:14 -0400224
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000225VariableWithLocation::~VariableWithLocation()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500226{
227}
Jamie Madill6a729792014-07-18 10:33:14 -0400228
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000229VariableWithLocation::VariableWithLocation(const VariableWithLocation &other)
Jamie Madilla0a9e122015-09-02 15:54:30 -0400230 : ShaderVariable(other), location(other.location)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500231{
232}
Jamie Madill6a729792014-07-18 10:33:14 -0400233
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000234VariableWithLocation &VariableWithLocation::operator=(const VariableWithLocation &other)
Jamie Madill6a729792014-07-18 10:33:14 -0400235{
236 ShaderVariable::operator=(other);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500237 location = other.location;
Jamie Madill6a729792014-07-18 10:33:14 -0400238 return *this;
239}
240
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000241bool VariableWithLocation::operator==(const VariableWithLocation &other) const
Zhenyao Moed136362014-10-03 13:23:01 -0700242{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500243 return (ShaderVariable::operator==(other) && location == other.location);
Zhenyao Moed136362014-10-03 13:23:01 -0700244}
245
Jamie Madilla0a9e122015-09-02 15:54:30 -0400246Attribute::Attribute()
247{
248}
249
250Attribute::~Attribute()
251{
252}
253
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000254Attribute::Attribute(const Attribute &other) : VariableWithLocation(other)
Jamie Madilla0a9e122015-09-02 15:54:30 -0400255{
256}
257
258Attribute &Attribute::operator=(const Attribute &other)
259{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000260 VariableWithLocation::operator=(other);
Jamie Madilla0a9e122015-09-02 15:54:30 -0400261 return *this;
262}
263
264bool Attribute::operator==(const Attribute &other) const
265{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000266 return VariableWithLocation::operator==(other);
Jamie Madilla0a9e122015-09-02 15:54:30 -0400267}
268
269OutputVariable::OutputVariable()
270{
271}
272
273OutputVariable::~OutputVariable()
274{
275}
276
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000277OutputVariable::OutputVariable(const OutputVariable &other) : VariableWithLocation(other)
Jamie Madilla0a9e122015-09-02 15:54:30 -0400278{
279}
280
281OutputVariable &OutputVariable::operator=(const OutputVariable &other)
282{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000283 VariableWithLocation::operator=(other);
Jamie Madilla0a9e122015-09-02 15:54:30 -0400284 return *this;
285}
286
287bool OutputVariable::operator==(const OutputVariable &other) const
288{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000289 return VariableWithLocation::operator==(other);
Jamie Madilla0a9e122015-09-02 15:54:30 -0400290}
291
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500292InterfaceBlockField::InterfaceBlockField() : isRowMajorLayout(false)
293{
294}
Jamie Madill6a729792014-07-18 10:33:14 -0400295
296InterfaceBlockField::~InterfaceBlockField()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500297{
298}
Jamie Madill6a729792014-07-18 10:33:14 -0400299
300InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500301 : ShaderVariable(other), isRowMajorLayout(other.isRowMajorLayout)
302{
303}
Jamie Madill6a729792014-07-18 10:33:14 -0400304
305InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other)
306{
307 ShaderVariable::operator=(other);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500308 isRowMajorLayout = other.isRowMajorLayout;
Jamie Madill6a729792014-07-18 10:33:14 -0400309 return *this;
310}
311
Zhenyao Moed136362014-10-03 13:23:01 -0700312bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const
313{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500314 return (ShaderVariable::operator==(other) && isRowMajorLayout == other.isRowMajorLayout);
Zhenyao Moed136362014-10-03 13:23:01 -0700315}
316
317bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime(
318 const InterfaceBlockField &other) const
319{
320 return (ShaderVariable::isSameVariableAtLinkTime(other, true) &&
321 isRowMajorLayout == other.isRowMajorLayout);
322}
323
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324Varying::Varying() : interpolation(INTERPOLATION_SMOOTH), isInvariant(false)
325{
326}
Jamie Madill6a729792014-07-18 10:33:14 -0400327
328Varying::~Varying()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500329{
330}
Jamie Madill6a729792014-07-18 10:33:14 -0400331
332Varying::Varying(const Varying &other)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500333 : ShaderVariable(other), interpolation(other.interpolation), isInvariant(other.isInvariant)
334{
335}
Jamie Madill6a729792014-07-18 10:33:14 -0400336
337Varying &Varying::operator=(const Varying &other)
338{
339 ShaderVariable::operator=(other);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500340 interpolation = other.interpolation;
341 isInvariant = other.isInvariant;
Jamie Madill6a729792014-07-18 10:33:14 -0400342 return *this;
343}
344
Zhenyao Moed136362014-10-03 13:23:01 -0700345bool Varying::operator==(const Varying &other) const
346{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500347 return (ShaderVariable::operator==(other) && interpolation == other.interpolation &&
Zhenyao Moed136362014-10-03 13:23:01 -0700348 isInvariant == other.isInvariant);
349}
350
Jamie Madill9e64edc2015-05-07 14:08:06 +0000351bool Varying::isSameVaryingAtLinkTime(const Varying &other) const
Zhenyao Moed136362014-10-03 13:23:01 -0700352{
Olli Etuaho37ad4742015-04-27 13:18:50 +0300353 return isSameVaryingAtLinkTime(other, 100);
354}
355
356bool Varying::isSameVaryingAtLinkTime(const Varying &other, int shaderVersion) const
357{
Zhenyao Moed136362014-10-03 13:23:01 -0700358 return (ShaderVariable::isSameVariableAtLinkTime(other, false) &&
Corentin Wallez84954982016-07-12 15:42:18 -0400359 InterpolationTypesMatch(interpolation, other.interpolation) &&
Olli Etuaho37ad4742015-04-27 13:18:50 +0300360 (shaderVersion >= 300 || isInvariant == other.isInvariant));
Zhenyao Moed136362014-10-03 13:23:01 -0700361}
362
Jamie Madill6a729792014-07-18 10:33:14 -0400363InterfaceBlock::InterfaceBlock()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500364 : arraySize(0), layout(BLOCKLAYOUT_PACKED), isRowMajorLayout(false), staticUse(false)
365{
366}
Jamie Madill6a729792014-07-18 10:33:14 -0400367
368InterfaceBlock::~InterfaceBlock()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500369{
370}
Jamie Madill6a729792014-07-18 10:33:14 -0400371
372InterfaceBlock::InterfaceBlock(const InterfaceBlock &other)
373 : name(other.name),
374 mappedName(other.mappedName),
Jamie Madill42bcf322014-08-25 16:20:46 -0400375 instanceName(other.instanceName),
Jamie Madill6a729792014-07-18 10:33:14 -0400376 arraySize(other.arraySize),
377 layout(other.layout),
378 isRowMajorLayout(other.isRowMajorLayout),
379 staticUse(other.staticUse),
380 fields(other.fields)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500381{
382}
Jamie Madill6a729792014-07-18 10:33:14 -0400383
384InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other)
385{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500386 name = other.name;
387 mappedName = other.mappedName;
388 instanceName = other.instanceName;
389 arraySize = other.arraySize;
390 layout = other.layout;
Jamie Madill6a729792014-07-18 10:33:14 -0400391 isRowMajorLayout = other.isRowMajorLayout;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500392 staticUse = other.staticUse;
393 fields = other.fields;
Jamie Madill6a729792014-07-18 10:33:14 -0400394 return *this;
395}
396
Jamie Madill39046162016-02-08 15:05:17 -0500397std::string InterfaceBlock::fieldPrefix() const
398{
399 return instanceName.empty() ? "" : name;
Jamie Madill6a729792014-07-18 10:33:14 -0400400}
Jamie Madill39046162016-02-08 15:05:17 -0500401
Corentin Wallez84954982016-07-12 15:42:18 -0400402bool InterfaceBlock::isSameInterfaceBlockAtLinkTime(const InterfaceBlock &other) const
403{
404 if (name != other.name || mappedName != other.mappedName || arraySize != other.arraySize ||
405 layout != other.layout || isRowMajorLayout != other.isRowMajorLayout ||
406 fields.size() != other.fields.size())
407 {
408 return false;
409 }
410
411 for (size_t fieldIndex = 0; fieldIndex < fields.size(); ++fieldIndex)
412 {
413 if (!fields[fieldIndex].isSameInterfaceBlockFieldAtLinkTime(other.fields[fieldIndex]))
414 {
415 return false;
416 }
417 }
418
419 return true;
420}
421
Martin Radev4c4c8e72016-08-04 12:25:34 +0300422void WorkGroupSize::fill(int fillValue)
423{
424 localSizeQualifiers[0] = fillValue;
425 localSizeQualifiers[1] = fillValue;
426 localSizeQualifiers[2] = fillValue;
427}
428
429void WorkGroupSize::setLocalSize(int localSizeX, int localSizeY, int localSizeZ)
430{
431 localSizeQualifiers[0] = localSizeX;
432 localSizeQualifiers[1] = localSizeY;
433 localSizeQualifiers[2] = localSizeZ;
434}
435
436// check that if one of them is less than 1, then all of them are.
437// Or if one is positive, then all of them are positive.
438bool WorkGroupSize::isLocalSizeValid() const
439{
440 return (
441 (localSizeQualifiers[0] < 1 && localSizeQualifiers[1] < 1 && localSizeQualifiers[2] < 1) ||
442 (localSizeQualifiers[0] > 0 && localSizeQualifiers[1] > 0 && localSizeQualifiers[2] > 0));
443}
444
445bool WorkGroupSize::isAnyValueSet() const
446{
447 return localSizeQualifiers[0] > 0 || localSizeQualifiers[1] > 0 || localSizeQualifiers[2] > 0;
448}
449
450bool WorkGroupSize::isDeclared() const
451{
452 bool localSizeDeclared = localSizeQualifiers[0] > 0;
453 ASSERT(isLocalSizeValid());
454 return localSizeDeclared;
455}
456
457bool WorkGroupSize::isWorkGroupSizeMatching(const WorkGroupSize &right) const
458{
459 for (size_t i = 0u; i < size(); ++i)
460 {
461 bool result = (localSizeQualifiers[i] == right.localSizeQualifiers[i] ||
462 (localSizeQualifiers[i] == 1 && right.localSizeQualifiers[i] == -1) ||
463 (localSizeQualifiers[i] == -1 && right.localSizeQualifiers[i] == 1));
464 if (!result)
465 {
466 return false;
467 }
468 }
469 return true;
470}
471
472int &WorkGroupSize::operator[](size_t index)
473{
474 ASSERT(index < size());
475 return localSizeQualifiers[index];
476}
477
478int WorkGroupSize::operator[](size_t index) const
479{
480 ASSERT(index < size());
481 return localSizeQualifiers[index];
482}
483
484size_t WorkGroupSize::size() const
485{
486 return 3u;
487}
488
Jamie Madill39046162016-02-08 15:05:17 -0500489} // namespace sh