blob: bc1fab34a3cc34ea8bf17b5c72d4a3ef1d82fe33 [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
Jamie Madill6a729792014-07-18 10:33:14 -0400184Uniform::Uniform()
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
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500192Uniform::Uniform(const Uniform &other) : ShaderVariable(other)
193{
194}
Jamie Madill6a729792014-07-18 10:33:14 -0400195
196Uniform &Uniform::operator=(const Uniform &other)
197{
198 ShaderVariable::operator=(other);
Jamie Madill6a729792014-07-18 10:33:14 -0400199 return *this;
200}
201
Zhenyao Moed136362014-10-03 13:23:01 -0700202bool Uniform::operator==(const Uniform &other) const
203{
204 return ShaderVariable::operator==(other);
205}
206
207bool Uniform::isSameUniformAtLinkTime(const Uniform &other) const
208{
209 return ShaderVariable::isSameVariableAtLinkTime(other, true);
210}
211
Jamie Madilla0a9e122015-09-02 15:54:30 -0400212InterfaceVariable::InterfaceVariable() : location(-1)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500213{
214}
Jamie Madill6a729792014-07-18 10:33:14 -0400215
Jamie Madilla0a9e122015-09-02 15:54:30 -0400216InterfaceVariable::~InterfaceVariable()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500217{
218}
Jamie Madill6a729792014-07-18 10:33:14 -0400219
Jamie Madilla0a9e122015-09-02 15:54:30 -0400220InterfaceVariable::InterfaceVariable(const InterfaceVariable &other)
221 : ShaderVariable(other), location(other.location)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500222{
223}
Jamie Madill6a729792014-07-18 10:33:14 -0400224
Jamie Madilla0a9e122015-09-02 15:54:30 -0400225InterfaceVariable &InterfaceVariable::operator=(const InterfaceVariable &other)
Jamie Madill6a729792014-07-18 10:33:14 -0400226{
227 ShaderVariable::operator=(other);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500228 location = other.location;
Jamie Madill6a729792014-07-18 10:33:14 -0400229 return *this;
230}
231
Jamie Madilla0a9e122015-09-02 15:54:30 -0400232bool InterfaceVariable::operator==(const InterfaceVariable &other) const
Zhenyao Moed136362014-10-03 13:23:01 -0700233{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500234 return (ShaderVariable::operator==(other) && location == other.location);
Zhenyao Moed136362014-10-03 13:23:01 -0700235}
236
Jamie Madilla0a9e122015-09-02 15:54:30 -0400237Attribute::Attribute()
238{
239}
240
241Attribute::~Attribute()
242{
243}
244
245Attribute::Attribute(const Attribute &other) : InterfaceVariable(other)
246{
247}
248
249Attribute &Attribute::operator=(const Attribute &other)
250{
251 InterfaceVariable::operator=(other);
252 return *this;
253}
254
255bool Attribute::operator==(const Attribute &other) const
256{
257 return InterfaceVariable::operator==(other);
258}
259
260OutputVariable::OutputVariable()
261{
262}
263
264OutputVariable::~OutputVariable()
265{
266}
267
268OutputVariable::OutputVariable(const OutputVariable &other) : InterfaceVariable(other)
269{
270}
271
272OutputVariable &OutputVariable::operator=(const OutputVariable &other)
273{
274 InterfaceVariable::operator=(other);
275 return *this;
276}
277
278bool OutputVariable::operator==(const OutputVariable &other) const
279{
280 return InterfaceVariable::operator==(other);
281}
282
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500283InterfaceBlockField::InterfaceBlockField() : isRowMajorLayout(false)
284{
285}
Jamie Madill6a729792014-07-18 10:33:14 -0400286
287InterfaceBlockField::~InterfaceBlockField()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500288{
289}
Jamie Madill6a729792014-07-18 10:33:14 -0400290
291InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500292 : ShaderVariable(other), isRowMajorLayout(other.isRowMajorLayout)
293{
294}
Jamie Madill6a729792014-07-18 10:33:14 -0400295
296InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other)
297{
298 ShaderVariable::operator=(other);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500299 isRowMajorLayout = other.isRowMajorLayout;
Jamie Madill6a729792014-07-18 10:33:14 -0400300 return *this;
301}
302
Zhenyao Moed136362014-10-03 13:23:01 -0700303bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const
304{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500305 return (ShaderVariable::operator==(other) && isRowMajorLayout == other.isRowMajorLayout);
Zhenyao Moed136362014-10-03 13:23:01 -0700306}
307
308bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime(
309 const InterfaceBlockField &other) const
310{
311 return (ShaderVariable::isSameVariableAtLinkTime(other, true) &&
312 isRowMajorLayout == other.isRowMajorLayout);
313}
314
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500315Varying::Varying() : interpolation(INTERPOLATION_SMOOTH), isInvariant(false)
316{
317}
Jamie Madill6a729792014-07-18 10:33:14 -0400318
319Varying::~Varying()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500320{
321}
Jamie Madill6a729792014-07-18 10:33:14 -0400322
323Varying::Varying(const Varying &other)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324 : ShaderVariable(other), interpolation(other.interpolation), isInvariant(other.isInvariant)
325{
326}
Jamie Madill6a729792014-07-18 10:33:14 -0400327
328Varying &Varying::operator=(const Varying &other)
329{
330 ShaderVariable::operator=(other);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500331 interpolation = other.interpolation;
332 isInvariant = other.isInvariant;
Jamie Madill6a729792014-07-18 10:33:14 -0400333 return *this;
334}
335
Zhenyao Moed136362014-10-03 13:23:01 -0700336bool Varying::operator==(const Varying &other) const
337{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500338 return (ShaderVariable::operator==(other) && interpolation == other.interpolation &&
Zhenyao Moed136362014-10-03 13:23:01 -0700339 isInvariant == other.isInvariant);
340}
341
Jamie Madill9e64edc2015-05-07 14:08:06 +0000342bool Varying::isSameVaryingAtLinkTime(const Varying &other) const
Zhenyao Moed136362014-10-03 13:23:01 -0700343{
Olli Etuaho37ad4742015-04-27 13:18:50 +0300344 return isSameVaryingAtLinkTime(other, 100);
345}
346
347bool Varying::isSameVaryingAtLinkTime(const Varying &other, int shaderVersion) const
348{
Zhenyao Moed136362014-10-03 13:23:01 -0700349 return (ShaderVariable::isSameVariableAtLinkTime(other, false) &&
Corentin Wallez84954982016-07-12 15:42:18 -0400350 InterpolationTypesMatch(interpolation, other.interpolation) &&
Olli Etuaho37ad4742015-04-27 13:18:50 +0300351 (shaderVersion >= 300 || isInvariant == other.isInvariant));
Zhenyao Moed136362014-10-03 13:23:01 -0700352}
353
Jamie Madill6a729792014-07-18 10:33:14 -0400354InterfaceBlock::InterfaceBlock()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500355 : arraySize(0), layout(BLOCKLAYOUT_PACKED), isRowMajorLayout(false), staticUse(false)
356{
357}
Jamie Madill6a729792014-07-18 10:33:14 -0400358
359InterfaceBlock::~InterfaceBlock()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500360{
361}
Jamie Madill6a729792014-07-18 10:33:14 -0400362
363InterfaceBlock::InterfaceBlock(const InterfaceBlock &other)
364 : name(other.name),
365 mappedName(other.mappedName),
Jamie Madill42bcf322014-08-25 16:20:46 -0400366 instanceName(other.instanceName),
Jamie Madill6a729792014-07-18 10:33:14 -0400367 arraySize(other.arraySize),
368 layout(other.layout),
369 isRowMajorLayout(other.isRowMajorLayout),
370 staticUse(other.staticUse),
371 fields(other.fields)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500372{
373}
Jamie Madill6a729792014-07-18 10:33:14 -0400374
375InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other)
376{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500377 name = other.name;
378 mappedName = other.mappedName;
379 instanceName = other.instanceName;
380 arraySize = other.arraySize;
381 layout = other.layout;
Jamie Madill6a729792014-07-18 10:33:14 -0400382 isRowMajorLayout = other.isRowMajorLayout;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500383 staticUse = other.staticUse;
384 fields = other.fields;
Jamie Madill6a729792014-07-18 10:33:14 -0400385 return *this;
386}
387
Jamie Madill39046162016-02-08 15:05:17 -0500388std::string InterfaceBlock::fieldPrefix() const
389{
390 return instanceName.empty() ? "" : name;
Jamie Madill6a729792014-07-18 10:33:14 -0400391}
Jamie Madill39046162016-02-08 15:05:17 -0500392
Corentin Wallez84954982016-07-12 15:42:18 -0400393bool InterfaceBlock::isSameInterfaceBlockAtLinkTime(const InterfaceBlock &other) const
394{
395 if (name != other.name || mappedName != other.mappedName || arraySize != other.arraySize ||
396 layout != other.layout || isRowMajorLayout != other.isRowMajorLayout ||
397 fields.size() != other.fields.size())
398 {
399 return false;
400 }
401
402 for (size_t fieldIndex = 0; fieldIndex < fields.size(); ++fieldIndex)
403 {
404 if (!fields[fieldIndex].isSameInterfaceBlockFieldAtLinkTime(other.fields[fieldIndex]))
405 {
406 return false;
407 }
408 }
409
410 return true;
411}
412
Martin Radev4c4c8e72016-08-04 12:25:34 +0300413void WorkGroupSize::fill(int fillValue)
414{
415 localSizeQualifiers[0] = fillValue;
416 localSizeQualifiers[1] = fillValue;
417 localSizeQualifiers[2] = fillValue;
418}
419
420void WorkGroupSize::setLocalSize(int localSizeX, int localSizeY, int localSizeZ)
421{
422 localSizeQualifiers[0] = localSizeX;
423 localSizeQualifiers[1] = localSizeY;
424 localSizeQualifiers[2] = localSizeZ;
425}
426
427// check that if one of them is less than 1, then all of them are.
428// Or if one is positive, then all of them are positive.
429bool WorkGroupSize::isLocalSizeValid() const
430{
431 return (
432 (localSizeQualifiers[0] < 1 && localSizeQualifiers[1] < 1 && localSizeQualifiers[2] < 1) ||
433 (localSizeQualifiers[0] > 0 && localSizeQualifiers[1] > 0 && localSizeQualifiers[2] > 0));
434}
435
436bool WorkGroupSize::isAnyValueSet() const
437{
438 return localSizeQualifiers[0] > 0 || localSizeQualifiers[1] > 0 || localSizeQualifiers[2] > 0;
439}
440
441bool WorkGroupSize::isDeclared() const
442{
443 bool localSizeDeclared = localSizeQualifiers[0] > 0;
444 ASSERT(isLocalSizeValid());
445 return localSizeDeclared;
446}
447
448bool WorkGroupSize::isWorkGroupSizeMatching(const WorkGroupSize &right) const
449{
450 for (size_t i = 0u; i < size(); ++i)
451 {
452 bool result = (localSizeQualifiers[i] == right.localSizeQualifiers[i] ||
453 (localSizeQualifiers[i] == 1 && right.localSizeQualifiers[i] == -1) ||
454 (localSizeQualifiers[i] == -1 && right.localSizeQualifiers[i] == 1));
455 if (!result)
456 {
457 return false;
458 }
459 }
460 return true;
461}
462
463int &WorkGroupSize::operator[](size_t index)
464{
465 ASSERT(index < size());
466 return localSizeQualifiers[index];
467}
468
469int WorkGroupSize::operator[](size_t index) const
470{
471 ASSERT(index < size());
472 return localSizeQualifiers[index];
473}
474
475size_t WorkGroupSize::size() const
476{
477 return 3u;
478}
479
Jamie Madill39046162016-02-08 15:05:17 -0500480} // namespace sh