blob: b40e61065971b3b47f1936271906dba747d6f701 [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 {
Yunchao Hed7297bf2017-04-19 15:27:10 +0800144 const ShaderVariable *fieldVar = nullptr;
Zhenyao Moed136362014-10-03 13:23:01 -0700145 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
jchen104cdac9e2017-05-08 11:01:20 +0800184Uniform::Uniform() : binding(-1), offset(-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
jchen104cdac9e2017-05-08 11:01:20 +0800192Uniform::Uniform(const Uniform &other)
193 : VariableWithLocation(other), binding(other.binding), offset(other.offset)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500194{
195}
Jamie Madill6a729792014-07-18 10:33:14 -0400196
197Uniform &Uniform::operator=(const Uniform &other)
198{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000199 VariableWithLocation::operator=(other);
Olli Etuaho547cbd42017-02-27 11:54:00 +0200200 binding = other.binding;
jchen104cdac9e2017-05-08 11:01:20 +0800201 offset = other.offset;
Jamie Madill6a729792014-07-18 10:33:14 -0400202 return *this;
203}
204
Zhenyao Moed136362014-10-03 13:23:01 -0700205bool Uniform::operator==(const Uniform &other) const
206{
jchen104cdac9e2017-05-08 11:01:20 +0800207 return VariableWithLocation::operator==(other) && binding == other.binding &&
208 offset == other.offset;
Zhenyao Moed136362014-10-03 13:23:01 -0700209}
210
211bool Uniform::isSameUniformAtLinkTime(const Uniform &other) const
212{
jchen104cdac9e2017-05-08 11:01:20 +0800213 if (binding != other.binding)
Olli Etuaho547cbd42017-02-27 11:54:00 +0200214 {
215 return false;
216 }
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000217 if (location != -1 && other.location != -1 && location != other.location)
218 {
219 return false;
220 }
jchen104cdac9e2017-05-08 11:01:20 +0800221 // TODO(jie.a.chen@intel.com): Add a test case to cover this.
222 if (offset != other.offset)
223 {
224 return false;
225 }
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000226 return VariableWithLocation::isSameVariableAtLinkTime(other, true);
Zhenyao Moed136362014-10-03 13:23:01 -0700227}
228
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000229VariableWithLocation::VariableWithLocation() : location(-1)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500230{
231}
Jamie Madill6a729792014-07-18 10:33:14 -0400232
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000233VariableWithLocation::~VariableWithLocation()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500234{
235}
Jamie Madill6a729792014-07-18 10:33:14 -0400236
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000237VariableWithLocation::VariableWithLocation(const VariableWithLocation &other)
Jamie Madilla0a9e122015-09-02 15:54:30 -0400238 : ShaderVariable(other), location(other.location)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500239{
240}
Jamie Madill6a729792014-07-18 10:33:14 -0400241
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000242VariableWithLocation &VariableWithLocation::operator=(const VariableWithLocation &other)
Jamie Madill6a729792014-07-18 10:33:14 -0400243{
244 ShaderVariable::operator=(other);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500245 location = other.location;
Jamie Madill6a729792014-07-18 10:33:14 -0400246 return *this;
247}
248
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000249bool VariableWithLocation::operator==(const VariableWithLocation &other) const
Zhenyao Moed136362014-10-03 13:23:01 -0700250{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500251 return (ShaderVariable::operator==(other) && location == other.location);
Zhenyao Moed136362014-10-03 13:23:01 -0700252}
253
Jamie Madilla0a9e122015-09-02 15:54:30 -0400254Attribute::Attribute()
255{
256}
257
258Attribute::~Attribute()
259{
260}
261
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000262Attribute::Attribute(const Attribute &other) : VariableWithLocation(other)
Jamie Madilla0a9e122015-09-02 15:54:30 -0400263{
264}
265
266Attribute &Attribute::operator=(const Attribute &other)
267{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000268 VariableWithLocation::operator=(other);
Jamie Madilla0a9e122015-09-02 15:54:30 -0400269 return *this;
270}
271
272bool Attribute::operator==(const Attribute &other) const
273{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000274 return VariableWithLocation::operator==(other);
Jamie Madilla0a9e122015-09-02 15:54:30 -0400275}
276
277OutputVariable::OutputVariable()
278{
279}
280
281OutputVariable::~OutputVariable()
282{
283}
284
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000285OutputVariable::OutputVariable(const OutputVariable &other) : VariableWithLocation(other)
Jamie Madilla0a9e122015-09-02 15:54:30 -0400286{
287}
288
289OutputVariable &OutputVariable::operator=(const OutputVariable &other)
290{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000291 VariableWithLocation::operator=(other);
Jamie Madilla0a9e122015-09-02 15:54:30 -0400292 return *this;
293}
294
295bool OutputVariable::operator==(const OutputVariable &other) const
296{
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000297 return VariableWithLocation::operator==(other);
Jamie Madilla0a9e122015-09-02 15:54:30 -0400298}
299
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500300InterfaceBlockField::InterfaceBlockField() : isRowMajorLayout(false)
301{
302}
Jamie Madill6a729792014-07-18 10:33:14 -0400303
304InterfaceBlockField::~InterfaceBlockField()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500305{
306}
Jamie Madill6a729792014-07-18 10:33:14 -0400307
308InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500309 : ShaderVariable(other), isRowMajorLayout(other.isRowMajorLayout)
310{
311}
Jamie Madill6a729792014-07-18 10:33:14 -0400312
313InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other)
314{
315 ShaderVariable::operator=(other);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500316 isRowMajorLayout = other.isRowMajorLayout;
Jamie Madill6a729792014-07-18 10:33:14 -0400317 return *this;
318}
319
Zhenyao Moed136362014-10-03 13:23:01 -0700320bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const
321{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500322 return (ShaderVariable::operator==(other) && isRowMajorLayout == other.isRowMajorLayout);
Zhenyao Moed136362014-10-03 13:23:01 -0700323}
324
325bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime(
326 const InterfaceBlockField &other) const
327{
328 return (ShaderVariable::isSameVariableAtLinkTime(other, true) &&
329 isRowMajorLayout == other.isRowMajorLayout);
330}
331
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500332Varying::Varying() : interpolation(INTERPOLATION_SMOOTH), isInvariant(false)
333{
334}
Jamie Madill6a729792014-07-18 10:33:14 -0400335
336Varying::~Varying()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500337{
338}
Jamie Madill6a729792014-07-18 10:33:14 -0400339
340Varying::Varying(const Varying &other)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500341 : ShaderVariable(other), interpolation(other.interpolation), isInvariant(other.isInvariant)
342{
343}
Jamie Madill6a729792014-07-18 10:33:14 -0400344
345Varying &Varying::operator=(const Varying &other)
346{
347 ShaderVariable::operator=(other);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500348 interpolation = other.interpolation;
349 isInvariant = other.isInvariant;
Jamie Madill6a729792014-07-18 10:33:14 -0400350 return *this;
351}
352
Zhenyao Moed136362014-10-03 13:23:01 -0700353bool Varying::operator==(const Varying &other) const
354{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500355 return (ShaderVariable::operator==(other) && interpolation == other.interpolation &&
Zhenyao Moed136362014-10-03 13:23:01 -0700356 isInvariant == other.isInvariant);
357}
358
Jamie Madill9e64edc2015-05-07 14:08:06 +0000359bool Varying::isSameVaryingAtLinkTime(const Varying &other) const
Zhenyao Moed136362014-10-03 13:23:01 -0700360{
Olli Etuaho37ad4742015-04-27 13:18:50 +0300361 return isSameVaryingAtLinkTime(other, 100);
362}
363
364bool Varying::isSameVaryingAtLinkTime(const Varying &other, int shaderVersion) const
365{
Zhenyao Moed136362014-10-03 13:23:01 -0700366 return (ShaderVariable::isSameVariableAtLinkTime(other, false) &&
Corentin Wallez84954982016-07-12 15:42:18 -0400367 InterpolationTypesMatch(interpolation, other.interpolation) &&
Olli Etuaho37ad4742015-04-27 13:18:50 +0300368 (shaderVersion >= 300 || isInvariant == other.isInvariant));
Zhenyao Moed136362014-10-03 13:23:01 -0700369}
370
Jamie Madill6a729792014-07-18 10:33:14 -0400371InterfaceBlock::InterfaceBlock()
jchen10af713a22017-04-19 09:10:56 +0800372 : arraySize(0),
373 layout(BLOCKLAYOUT_PACKED),
374 isRowMajorLayout(false),
375 binding(-1),
376 staticUse(false)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500377{
378}
Jamie Madill6a729792014-07-18 10:33:14 -0400379
380InterfaceBlock::~InterfaceBlock()
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500381{
382}
Jamie Madill6a729792014-07-18 10:33:14 -0400383
384InterfaceBlock::InterfaceBlock(const InterfaceBlock &other)
385 : name(other.name),
386 mappedName(other.mappedName),
Jamie Madill42bcf322014-08-25 16:20:46 -0400387 instanceName(other.instanceName),
Jamie Madill6a729792014-07-18 10:33:14 -0400388 arraySize(other.arraySize),
389 layout(other.layout),
390 isRowMajorLayout(other.isRowMajorLayout),
jchen10af713a22017-04-19 09:10:56 +0800391 binding(other.binding),
Jamie Madill6a729792014-07-18 10:33:14 -0400392 staticUse(other.staticUse),
393 fields(other.fields)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500394{
395}
Jamie Madill6a729792014-07-18 10:33:14 -0400396
397InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other)
398{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500399 name = other.name;
400 mappedName = other.mappedName;
401 instanceName = other.instanceName;
402 arraySize = other.arraySize;
403 layout = other.layout;
Jamie Madill6a729792014-07-18 10:33:14 -0400404 isRowMajorLayout = other.isRowMajorLayout;
jchen10af713a22017-04-19 09:10:56 +0800405 binding = other.binding;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500406 staticUse = other.staticUse;
407 fields = other.fields;
Jamie Madill6a729792014-07-18 10:33:14 -0400408 return *this;
409}
410
Jamie Madill39046162016-02-08 15:05:17 -0500411std::string InterfaceBlock::fieldPrefix() const
412{
413 return instanceName.empty() ? "" : name;
Jamie Madill6a729792014-07-18 10:33:14 -0400414}
Jamie Madill39046162016-02-08 15:05:17 -0500415
Corentin Wallez84954982016-07-12 15:42:18 -0400416bool InterfaceBlock::isSameInterfaceBlockAtLinkTime(const InterfaceBlock &other) const
417{
418 if (name != other.name || mappedName != other.mappedName || arraySize != other.arraySize ||
419 layout != other.layout || isRowMajorLayout != other.isRowMajorLayout ||
jchen10af713a22017-04-19 09:10:56 +0800420 binding != other.binding || fields.size() != other.fields.size())
Corentin Wallez84954982016-07-12 15:42:18 -0400421 {
422 return false;
423 }
424
425 for (size_t fieldIndex = 0; fieldIndex < fields.size(); ++fieldIndex)
426 {
427 if (!fields[fieldIndex].isSameInterfaceBlockFieldAtLinkTime(other.fields[fieldIndex]))
428 {
429 return false;
430 }
431 }
432
433 return true;
434}
435
Martin Radev4c4c8e72016-08-04 12:25:34 +0300436void WorkGroupSize::fill(int fillValue)
437{
438 localSizeQualifiers[0] = fillValue;
439 localSizeQualifiers[1] = fillValue;
440 localSizeQualifiers[2] = fillValue;
441}
442
443void WorkGroupSize::setLocalSize(int localSizeX, int localSizeY, int localSizeZ)
444{
445 localSizeQualifiers[0] = localSizeX;
446 localSizeQualifiers[1] = localSizeY;
447 localSizeQualifiers[2] = localSizeZ;
448}
449
450// check that if one of them is less than 1, then all of them are.
451// Or if one is positive, then all of them are positive.
452bool WorkGroupSize::isLocalSizeValid() const
453{
454 return (
455 (localSizeQualifiers[0] < 1 && localSizeQualifiers[1] < 1 && localSizeQualifiers[2] < 1) ||
456 (localSizeQualifiers[0] > 0 && localSizeQualifiers[1] > 0 && localSizeQualifiers[2] > 0));
457}
458
459bool WorkGroupSize::isAnyValueSet() const
460{
461 return localSizeQualifiers[0] > 0 || localSizeQualifiers[1] > 0 || localSizeQualifiers[2] > 0;
462}
463
464bool WorkGroupSize::isDeclared() const
465{
466 bool localSizeDeclared = localSizeQualifiers[0] > 0;
467 ASSERT(isLocalSizeValid());
468 return localSizeDeclared;
469}
470
471bool WorkGroupSize::isWorkGroupSizeMatching(const WorkGroupSize &right) const
472{
473 for (size_t i = 0u; i < size(); ++i)
474 {
475 bool result = (localSizeQualifiers[i] == right.localSizeQualifiers[i] ||
476 (localSizeQualifiers[i] == 1 && right.localSizeQualifiers[i] == -1) ||
477 (localSizeQualifiers[i] == -1 && right.localSizeQualifiers[i] == 1));
478 if (!result)
479 {
480 return false;
481 }
482 }
483 return true;
484}
485
486int &WorkGroupSize::operator[](size_t index)
487{
488 ASSERT(index < size());
489 return localSizeQualifiers[index];
490}
491
492int WorkGroupSize::operator[](size_t index) const
493{
494 ASSERT(index < size());
495 return localSizeQualifiers[index];
496}
497
498size_t WorkGroupSize::size() const
499{
500 return 3u;
501}
502
Jamie Madill39046162016-02-08 15:05:17 -0500503} // namespace sh