blob: 187b73ee77332ba320e7c606db15352ec3145c4f [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(".[");
Zhenyao Moed136362014-10-03 13:23:01 -070089
90 if (pos == std::string::npos)
91 {
92 // Case 3.
93 if (mappedFullName != this->mappedName)
94 return false;
95 *originalFullName = this->name;
96 *leafVar = this;
97 return true;
98 }
99 else
100 {
101 std::string topName = mappedFullName.substr(0, pos);
102 if (topName != this->mappedName)
103 return false;
104 std::string originalName = this->name;
105 std::string remaining;
106 if (mappedFullName[pos] == '[')
107 {
108 // Case 2.
109 size_t closePos = mappedFullName.find_first_of(']');
110 if (closePos < pos || closePos == std::string::npos)
111 return false;
112 // Append '[index]'.
113 originalName += mappedFullName.substr(pos, closePos - pos + 1);
114 if (closePos + 1 == mappedFullName.size())
115 {
116 *originalFullName = originalName;
117 *leafVar = this;
118 return true;
119 }
120 else
121 {
122 // In the form of 'a[0].b', so after ']', '.' is expected.
123 if (mappedFullName[closePos + 1] != '.')
124 return false;
125 remaining = mappedFullName.substr(closePos + 2); // Skip "]."
126 }
127 }
128 else
129 {
130 // Case 1.
131 remaining = mappedFullName.substr(pos + 1); // Skip "."
132 }
133 for (size_t ii = 0; ii < this->fields.size(); ++ii)
134 {
135 const ShaderVariable *fieldVar = NULL;
136 std::string originalFieldName;
137 bool found = fields[ii].findInfoByMappedName(
138 remaining, &fieldVar, &originalFieldName);
139 if (found)
140 {
141 *originalFullName = originalName + "." + originalFieldName;
142 *leafVar = fieldVar;
143 return true;
144 }
145 }
146 return false;
147 }
148}
149
150bool ShaderVariable::isSameVariableAtLinkTime(
151 const ShaderVariable &other, bool matchPrecision) const
152{
153 if (type != other.type)
154 return false;
155 if (matchPrecision && precision != other.precision)
156 return false;
157 if (name != other.name)
158 return false;
159 ASSERT(mappedName == other.mappedName);
160 if (arraySize != other.arraySize)
161 return false;
162 if (fields.size() != other.fields.size())
163 return false;
164 for (size_t ii = 0; ii < fields.size(); ++ii)
165 {
166 if (!fields[ii].isSameVariableAtLinkTime(other.fields[ii],
167 matchPrecision))
168 {
169 return false;
170 }
171 }
172 if (structName != other.structName)
173 return false;
174 return true;
175}
176
Jamie Madill6a729792014-07-18 10:33:14 -0400177Uniform::Uniform()
178{}
179
180Uniform::~Uniform()
181{}
182
183Uniform::Uniform(const Uniform &other)
Jamie Madill42bcf322014-08-25 16:20:46 -0400184 : ShaderVariable(other)
Jamie Madill6a729792014-07-18 10:33:14 -0400185{}
186
187Uniform &Uniform::operator=(const Uniform &other)
188{
189 ShaderVariable::operator=(other);
Jamie Madill6a729792014-07-18 10:33:14 -0400190 return *this;
191}
192
Zhenyao Moed136362014-10-03 13:23:01 -0700193bool Uniform::operator==(const Uniform &other) const
194{
195 return ShaderVariable::operator==(other);
196}
197
198bool Uniform::isSameUniformAtLinkTime(const Uniform &other) const
199{
200 return ShaderVariable::isSameVariableAtLinkTime(other, true);
201}
202
Jamie Madill6a729792014-07-18 10:33:14 -0400203Attribute::Attribute()
204 : location(-1)
205{}
206
207Attribute::~Attribute()
208{}
209
210Attribute::Attribute(const Attribute &other)
211 : ShaderVariable(other),
212 location(other.location)
213{}
214
215Attribute &Attribute::operator=(const Attribute &other)
216{
217 ShaderVariable::operator=(other);
218 location = other.location;
219 return *this;
220}
221
Zhenyao Moed136362014-10-03 13:23:01 -0700222bool Attribute::operator==(const Attribute &other) const
223{
224 return (ShaderVariable::operator==(other) &&
225 location == other.location);
226}
227
Jamie Madill6a729792014-07-18 10:33:14 -0400228InterfaceBlockField::InterfaceBlockField()
Jamie Madilla6f267f2014-08-27 11:44:15 -0400229 : isRowMajorLayout(false)
Jamie Madill6a729792014-07-18 10:33:14 -0400230{}
231
232InterfaceBlockField::~InterfaceBlockField()
233{}
234
235InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other)
236 : ShaderVariable(other),
Jamie Madilla6f267f2014-08-27 11:44:15 -0400237 isRowMajorLayout(other.isRowMajorLayout)
Jamie Madill6a729792014-07-18 10:33:14 -0400238{}
239
240InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other)
241{
242 ShaderVariable::operator=(other);
Jamie Madilla6f267f2014-08-27 11:44:15 -0400243 isRowMajorLayout = other.isRowMajorLayout;
Jamie Madill6a729792014-07-18 10:33:14 -0400244 return *this;
245}
246
Zhenyao Moed136362014-10-03 13:23:01 -0700247bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const
248{
249 return (ShaderVariable::operator==(other) &&
250 isRowMajorLayout == other.isRowMajorLayout);
251}
252
253bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime(
254 const InterfaceBlockField &other) const
255{
256 return (ShaderVariable::isSameVariableAtLinkTime(other, true) &&
257 isRowMajorLayout == other.isRowMajorLayout);
258}
259
Jamie Madill6a729792014-07-18 10:33:14 -0400260Varying::Varying()
Jamie Madill42bcf322014-08-25 16:20:46 -0400261 : interpolation(INTERPOLATION_SMOOTH),
262 isInvariant(false)
Jamie Madill6a729792014-07-18 10:33:14 -0400263{}
264
265Varying::~Varying()
266{}
267
268Varying::Varying(const Varying &other)
269 : ShaderVariable(other),
270 interpolation(other.interpolation),
Jamie Madill42bcf322014-08-25 16:20:46 -0400271 isInvariant(other.isInvariant)
Jamie Madill6a729792014-07-18 10:33:14 -0400272{}
273
274Varying &Varying::operator=(const Varying &other)
275{
276 ShaderVariable::operator=(other);
277 interpolation = other.interpolation;
Jamie Madill42bcf322014-08-25 16:20:46 -0400278 isInvariant = other.isInvariant;
Jamie Madill6a729792014-07-18 10:33:14 -0400279 return *this;
280}
281
Zhenyao Moed136362014-10-03 13:23:01 -0700282bool Varying::operator==(const Varying &other) const
283{
284 return (ShaderVariable::operator==(other) &&
285 interpolation == other.interpolation &&
286 isInvariant == other.isInvariant);
287}
288
289bool Varying::isSameVaryingAtLinkTime(const Varying &other) const
290{
291 return (ShaderVariable::isSameVariableAtLinkTime(other, false) &&
292 interpolation == other.interpolation &&
293 isInvariant == other.isInvariant);
294}
295
Jamie Madill6a729792014-07-18 10:33:14 -0400296InterfaceBlock::InterfaceBlock()
297 : arraySize(0),
298 layout(BLOCKLAYOUT_PACKED),
299 isRowMajorLayout(false),
300 staticUse(false)
301{}
302
303InterfaceBlock::~InterfaceBlock()
304{}
305
306InterfaceBlock::InterfaceBlock(const InterfaceBlock &other)
307 : name(other.name),
308 mappedName(other.mappedName),
Jamie Madill42bcf322014-08-25 16:20:46 -0400309 instanceName(other.instanceName),
Jamie Madill6a729792014-07-18 10:33:14 -0400310 arraySize(other.arraySize),
311 layout(other.layout),
312 isRowMajorLayout(other.isRowMajorLayout),
313 staticUse(other.staticUse),
314 fields(other.fields)
315{}
316
317InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other)
318{
319 name = other.name;
320 mappedName = other.mappedName;
Jamie Madill42bcf322014-08-25 16:20:46 -0400321 instanceName = other.instanceName;
Jamie Madill6a729792014-07-18 10:33:14 -0400322 arraySize = other.arraySize;
323 layout = other.layout;
324 isRowMajorLayout = other.isRowMajorLayout;
325 staticUse = other.staticUse;
326 fields = other.fields;
327 return *this;
328}
329
330}