blob: 3098a7f0c9553e604d572762aa343ae15f622250 [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
Zhenyao Moed136362014-10-03 13:23:01 -070012#include "compiler/translator/compilerdebug.h"
13
Jamie Madill6a729792014-07-18 10:33:14 -040014namespace sh
15{
16
17ShaderVariable::ShaderVariable()
18 : type(0),
19 precision(0),
20 arraySize(0),
21 staticUse(false)
22{}
23
24ShaderVariable::ShaderVariable(GLenum typeIn, unsigned int arraySizeIn)
25 : type(typeIn),
26 precision(0),
27 arraySize(arraySizeIn),
28 staticUse(false)
29{}
30
31ShaderVariable::~ShaderVariable()
32{}
33
34ShaderVariable::ShaderVariable(const ShaderVariable &other)
35 : type(other.type),
36 precision(other.precision),
37 name(other.name),
38 mappedName(other.mappedName),
39 arraySize(other.arraySize),
Jamie Madill42bcf322014-08-25 16:20:46 -040040 staticUse(other.staticUse),
41 fields(other.fields),
42 structName(other.structName)
Jamie Madill6a729792014-07-18 10:33:14 -040043{}
44
45ShaderVariable &ShaderVariable::operator=(const ShaderVariable &other)
46{
47 type = other.type;
48 precision = other.precision;
49 name = other.name;
50 mappedName = other.mappedName;
51 arraySize = other.arraySize;
52 staticUse = other.staticUse;
Jamie Madill42bcf322014-08-25 16:20:46 -040053 fields = other.fields;
54 structName = other.structName;
Jamie Madill6a729792014-07-18 10:33:14 -040055 return *this;
56}
57
Zhenyao Moed136362014-10-03 13:23:01 -070058bool ShaderVariable::operator==(const ShaderVariable &other) const
59{
60 if (type != other.type ||
61 precision != other.precision ||
62 name != other.name ||
63 mappedName != other.mappedName ||
64 arraySize != other.arraySize ||
65 staticUse != other.staticUse ||
66 fields.size() != other.fields.size() ||
67 structName != other.structName)
68 {
69 return false;
70 }
71 for (size_t ii = 0; ii < fields.size(); ++ii)
72 {
73 if (fields[ii] != other.fields[ii])
74 return false;
75 }
76 return true;
77}
78
79bool ShaderVariable::findInfoByMappedName(
80 const std::string &mappedFullName,
81 const ShaderVariable **leafVar, std::string *originalFullName) const
82{
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;
87 // 3) otherwise.
88 size_t pos = mappedFullName.find_first_of(".[");
89 std::string topName;
90
91 if (pos == std::string::npos)
92 {
93 // Case 3.
94 if (mappedFullName != this->mappedName)
95 return false;
96 *originalFullName = this->name;
97 *leafVar = this;
98 return true;
99 }
100 else
101 {
102 std::string topName = mappedFullName.substr(0, pos);
103 if (topName != this->mappedName)
104 return false;
105 std::string originalName = this->name;
106 std::string remaining;
107 if (mappedFullName[pos] == '[')
108 {
109 // Case 2.
110 size_t closePos = mappedFullName.find_first_of(']');
111 if (closePos < pos || closePos == std::string::npos)
112 return false;
113 // Append '[index]'.
114 originalName += mappedFullName.substr(pos, closePos - pos + 1);
115 if (closePos + 1 == mappedFullName.size())
116 {
117 *originalFullName = originalName;
118 *leafVar = this;
119 return true;
120 }
121 else
122 {
123 // In the form of 'a[0].b', so after ']', '.' is expected.
124 if (mappedFullName[closePos + 1] != '.')
125 return false;
126 remaining = mappedFullName.substr(closePos + 2); // Skip "]."
127 }
128 }
129 else
130 {
131 // Case 1.
132 remaining = mappedFullName.substr(pos + 1); // Skip "."
133 }
134 for (size_t ii = 0; ii < this->fields.size(); ++ii)
135 {
136 const ShaderVariable *fieldVar = NULL;
137 std::string originalFieldName;
138 bool found = fields[ii].findInfoByMappedName(
139 remaining, &fieldVar, &originalFieldName);
140 if (found)
141 {
142 *originalFullName = originalName + "." + originalFieldName;
143 *leafVar = fieldVar;
144 return true;
145 }
146 }
147 return false;
148 }
149}
150
151bool ShaderVariable::isSameVariableAtLinkTime(
152 const ShaderVariable &other, bool matchPrecision) const
153{
154 if (type != other.type)
155 return false;
156 if (matchPrecision && precision != other.precision)
157 return false;
158 if (name != other.name)
159 return false;
160 ASSERT(mappedName == other.mappedName);
161 if (arraySize != other.arraySize)
162 return false;
163 if (fields.size() != other.fields.size())
164 return false;
165 for (size_t ii = 0; ii < fields.size(); ++ii)
166 {
167 if (!fields[ii].isSameVariableAtLinkTime(other.fields[ii],
168 matchPrecision))
169 {
170 return false;
171 }
172 }
173 if (structName != other.structName)
174 return false;
175 return true;
176}
177
Jamie Madill6a729792014-07-18 10:33:14 -0400178Uniform::Uniform()
179{}
180
181Uniform::~Uniform()
182{}
183
184Uniform::Uniform(const Uniform &other)
Jamie Madill42bcf322014-08-25 16:20:46 -0400185 : ShaderVariable(other)
Jamie Madill6a729792014-07-18 10:33:14 -0400186{}
187
188Uniform &Uniform::operator=(const Uniform &other)
189{
190 ShaderVariable::operator=(other);
Jamie Madill6a729792014-07-18 10:33:14 -0400191 return *this;
192}
193
Zhenyao Moed136362014-10-03 13:23:01 -0700194bool Uniform::operator==(const Uniform &other) const
195{
196 return ShaderVariable::operator==(other);
197}
198
199bool Uniform::isSameUniformAtLinkTime(const Uniform &other) const
200{
201 return ShaderVariable::isSameVariableAtLinkTime(other, true);
202}
203
Jamie Madill6a729792014-07-18 10:33:14 -0400204Attribute::Attribute()
205 : location(-1)
206{}
207
208Attribute::~Attribute()
209{}
210
211Attribute::Attribute(const Attribute &other)
212 : ShaderVariable(other),
213 location(other.location)
214{}
215
216Attribute &Attribute::operator=(const Attribute &other)
217{
218 ShaderVariable::operator=(other);
219 location = other.location;
220 return *this;
221}
222
Zhenyao Moed136362014-10-03 13:23:01 -0700223bool Attribute::operator==(const Attribute &other) const
224{
225 return (ShaderVariable::operator==(other) &&
226 location == other.location);
227}
228
Jamie Madill6a729792014-07-18 10:33:14 -0400229InterfaceBlockField::InterfaceBlockField()
Jamie Madilla6f267f2014-08-27 11:44:15 -0400230 : isRowMajorLayout(false)
Jamie Madill6a729792014-07-18 10:33:14 -0400231{}
232
233InterfaceBlockField::~InterfaceBlockField()
234{}
235
236InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other)
237 : ShaderVariable(other),
Jamie Madilla6f267f2014-08-27 11:44:15 -0400238 isRowMajorLayout(other.isRowMajorLayout)
Jamie Madill6a729792014-07-18 10:33:14 -0400239{}
240
241InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other)
242{
243 ShaderVariable::operator=(other);
Jamie Madilla6f267f2014-08-27 11:44:15 -0400244 isRowMajorLayout = other.isRowMajorLayout;
Jamie Madill6a729792014-07-18 10:33:14 -0400245 return *this;
246}
247
Zhenyao Moed136362014-10-03 13:23:01 -0700248bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const
249{
250 return (ShaderVariable::operator==(other) &&
251 isRowMajorLayout == other.isRowMajorLayout);
252}
253
254bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime(
255 const InterfaceBlockField &other) const
256{
257 return (ShaderVariable::isSameVariableAtLinkTime(other, true) &&
258 isRowMajorLayout == other.isRowMajorLayout);
259}
260
Jamie Madill6a729792014-07-18 10:33:14 -0400261Varying::Varying()
Jamie Madill42bcf322014-08-25 16:20:46 -0400262 : interpolation(INTERPOLATION_SMOOTH),
263 isInvariant(false)
Jamie Madill6a729792014-07-18 10:33:14 -0400264{}
265
266Varying::~Varying()
267{}
268
269Varying::Varying(const Varying &other)
270 : ShaderVariable(other),
271 interpolation(other.interpolation),
Jamie Madill42bcf322014-08-25 16:20:46 -0400272 isInvariant(other.isInvariant)
Jamie Madill6a729792014-07-18 10:33:14 -0400273{}
274
275Varying &Varying::operator=(const Varying &other)
276{
277 ShaderVariable::operator=(other);
278 interpolation = other.interpolation;
Jamie Madill42bcf322014-08-25 16:20:46 -0400279 isInvariant = other.isInvariant;
Jamie Madill6a729792014-07-18 10:33:14 -0400280 return *this;
281}
282
Zhenyao Moed136362014-10-03 13:23:01 -0700283bool Varying::operator==(const Varying &other) const
284{
285 return (ShaderVariable::operator==(other) &&
286 interpolation == other.interpolation &&
287 isInvariant == other.isInvariant);
288}
289
290bool Varying::isSameVaryingAtLinkTime(const Varying &other) const
291{
292 return (ShaderVariable::isSameVariableAtLinkTime(other, false) &&
293 interpolation == other.interpolation &&
294 isInvariant == other.isInvariant);
295}
296
Jamie Madill6a729792014-07-18 10:33:14 -0400297InterfaceBlock::InterfaceBlock()
298 : arraySize(0),
299 layout(BLOCKLAYOUT_PACKED),
300 isRowMajorLayout(false),
301 staticUse(false)
302{}
303
304InterfaceBlock::~InterfaceBlock()
305{}
306
307InterfaceBlock::InterfaceBlock(const InterfaceBlock &other)
308 : name(other.name),
309 mappedName(other.mappedName),
Jamie Madill42bcf322014-08-25 16:20:46 -0400310 instanceName(other.instanceName),
Jamie Madill6a729792014-07-18 10:33:14 -0400311 arraySize(other.arraySize),
312 layout(other.layout),
313 isRowMajorLayout(other.isRowMajorLayout),
314 staticUse(other.staticUse),
315 fields(other.fields)
316{}
317
318InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other)
319{
320 name = other.name;
321 mappedName = other.mappedName;
Jamie Madill42bcf322014-08-25 16:20:46 -0400322 instanceName = other.instanceName;
Jamie Madill6a729792014-07-18 10:33:14 -0400323 arraySize = other.arraySize;
324 layout = other.layout;
325 isRowMajorLayout = other.isRowMajorLayout;
326 staticUse = other.staticUse;
327 fields = other.fields;
328 return *this;
329}
330
331}