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.
7 // Methods for GL variable types (varyings, uniforms, etc)
10 #include <GLSLANG/ShaderLang.h>
12 #include "compiler/translator/compilerdebug.h"
17 ShaderVariable::ShaderVariable()
24 ShaderVariable::ShaderVariable(GLenum typeIn, unsigned int arraySizeIn)
27 arraySize(arraySizeIn),
31 ShaderVariable::~ShaderVariable()
34 ShaderVariable::ShaderVariable(const ShaderVariable &other)
36 precision(other.precision),
38 mappedName(other.mappedName),
39 arraySize(other.arraySize),
40 staticUse(other.staticUse),
42 structName(other.structName)
45 ShaderVariable &ShaderVariable::operator=(const ShaderVariable &other)
48 precision = other.precision;
50 mappedName = other.mappedName;
51 arraySize = other.arraySize;
52 staticUse = other.staticUse;
53 fields = other.fields;
54 structName = other.structName;
58 bool ShaderVariable::operator==(const ShaderVariable &other) const
60 if (type != other.type ||
61 precision != other.precision ||
63 mappedName != other.mappedName ||
64 arraySize != other.arraySize ||
65 staticUse != other.staticUse ||
66 fields.size() != other.fields.size() ||
67 structName != other.structName)
71 for (size_t ii = 0; ii < fields.size(); ++ii)
73 if (fields[ii] != other.fields[ii])
79 bool ShaderVariable::findInfoByMappedName(
80 const std::string &mappedFullName,
81 const ShaderVariable **leafVar, std::string *originalFullName) const
83 ASSERT(leafVar && originalFullName);
84 // There are three cases:
85 // 1) the top variable is of struct type;
86 // 2) the top variable is an array;
88 size_t pos = mappedFullName.find_first_of(".[");
91 if (pos == std::string::npos)
94 if (mappedFullName != this->mappedName)
96 *originalFullName = this->name;
102 std::string topName = mappedFullName.substr(0, pos);
103 if (topName != this->mappedName)
105 std::string originalName = this->name;
106 std::string remaining;
107 if (mappedFullName[pos] == '[')
110 size_t closePos = mappedFullName.find_first_of(']');
111 if (closePos < pos || closePos == std::string::npos)
114 originalName += mappedFullName.substr(pos, closePos - pos + 1);
115 if (closePos + 1 == mappedFullName.size())
117 *originalFullName = originalName;
123 // In the form of 'a[0].b', so after ']', '.' is expected.
124 if (mappedFullName[closePos + 1] != '.')
126 remaining = mappedFullName.substr(closePos + 2); // Skip "]."
132 remaining = mappedFullName.substr(pos + 1); // Skip "."
134 for (size_t ii = 0; ii < this->fields.size(); ++ii)
136 const ShaderVariable *fieldVar = NULL;
137 std::string originalFieldName;
138 bool found = fields[ii].findInfoByMappedName(
139 remaining, &fieldVar, &originalFieldName);
142 *originalFullName = originalName + "." + originalFieldName;
151 bool ShaderVariable::isSameVariableAtLinkTime(
152 const ShaderVariable &other, bool matchPrecision) const
154 if (type != other.type)
156 if (matchPrecision && precision != other.precision)
158 if (name != other.name)
160 ASSERT(mappedName == other.mappedName);
161 if (arraySize != other.arraySize)
163 if (fields.size() != other.fields.size())
165 for (size_t ii = 0; ii < fields.size(); ++ii)
167 if (!fields[ii].isSameVariableAtLinkTime(other.fields[ii],
173 if (structName != other.structName)
184 Uniform::Uniform(const Uniform &other)
185 : ShaderVariable(other)
188 Uniform &Uniform::operator=(const Uniform &other)
190 ShaderVariable::operator=(other);
194 bool Uniform::operator==(const Uniform &other) const
196 return ShaderVariable::operator==(other);
199 bool Uniform::isSameUniformAtLinkTime(const Uniform &other) const
201 return ShaderVariable::isSameVariableAtLinkTime(other, true);
204 Attribute::Attribute()
208 Attribute::~Attribute()
211 Attribute::Attribute(const Attribute &other)
212 : ShaderVariable(other),
213 location(other.location)
216 Attribute &Attribute::operator=(const Attribute &other)
218 ShaderVariable::operator=(other);
219 location = other.location;
223 bool Attribute::operator==(const Attribute &other) const
225 return (ShaderVariable::operator==(other) &&
226 location == other.location);
229 InterfaceBlockField::InterfaceBlockField()
230 : isRowMajorLayout(false)
233 InterfaceBlockField::~InterfaceBlockField()
236 InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other)
237 : ShaderVariable(other),
238 isRowMajorLayout(other.isRowMajorLayout)
241 InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other)
243 ShaderVariable::operator=(other);
244 isRowMajorLayout = other.isRowMajorLayout;
248 bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const
250 return (ShaderVariable::operator==(other) &&
251 isRowMajorLayout == other.isRowMajorLayout);
254 bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime(
255 const InterfaceBlockField &other) const
257 return (ShaderVariable::isSameVariableAtLinkTime(other, true) &&
258 isRowMajorLayout == other.isRowMajorLayout);
262 : interpolation(INTERPOLATION_SMOOTH),
269 Varying::Varying(const Varying &other)
270 : ShaderVariable(other),
271 interpolation(other.interpolation),
272 isInvariant(other.isInvariant)
275 Varying &Varying::operator=(const Varying &other)
277 ShaderVariable::operator=(other);
278 interpolation = other.interpolation;
279 isInvariant = other.isInvariant;
283 bool Varying::operator==(const Varying &other) const
285 return (ShaderVariable::operator==(other) &&
286 interpolation == other.interpolation &&
287 isInvariant == other.isInvariant);
290 bool Varying::isSameVaryingAtLinkTime(const Varying &other) const
292 return (ShaderVariable::isSameVariableAtLinkTime(other, false) &&
293 interpolation == other.interpolation &&
294 isInvariant == other.isInvariant);
297 InterfaceBlock::InterfaceBlock()
299 layout(BLOCKLAYOUT_PACKED),
300 isRowMajorLayout(false),
304 InterfaceBlock::~InterfaceBlock()
307 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other)
309 mappedName(other.mappedName),
310 instanceName(other.instanceName),
311 arraySize(other.arraySize),
312 layout(other.layout),
313 isRowMajorLayout(other.isRowMajorLayout),
314 staticUse(other.staticUse),
318 InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other)
321 mappedName = other.mappedName;
322 instanceName = other.instanceName;
323 arraySize = other.arraySize;
324 layout = other.layout;
325 isRowMajorLayout = other.isRowMajorLayout;
326 staticUse = other.staticUse;
327 fields = other.fields;