Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 1 | // |
| 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 Madill | a2ad4e8 | 2014-07-17 14:16:32 -0400 | [diff] [blame] | 6 | // ShaderVars.cpp: |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 7 | // Methods for GL variable types (varyings, uniforms, etc) |
| 8 | // |
| 9 | |
Jamie Madill | e294bb8 | 2014-07-17 14:16:26 -0400 | [diff] [blame] | 10 | #include <GLSLANG/ShaderLang.h> |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 11 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 12 | #include "compiler/translator/compilerdebug.h" |
| 13 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 14 | namespace sh |
| 15 | { |
| 16 | |
| 17 | ShaderVariable::ShaderVariable() |
| 18 | : type(0), |
| 19 | precision(0), |
| 20 | arraySize(0), |
| 21 | staticUse(false) |
| 22 | {} |
| 23 | |
| 24 | ShaderVariable::ShaderVariable(GLenum typeIn, unsigned int arraySizeIn) |
| 25 | : type(typeIn), |
| 26 | precision(0), |
| 27 | arraySize(arraySizeIn), |
| 28 | staticUse(false) |
| 29 | {} |
| 30 | |
| 31 | ShaderVariable::~ShaderVariable() |
| 32 | {} |
| 33 | |
| 34 | ShaderVariable::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 Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 40 | staticUse(other.staticUse), |
| 41 | fields(other.fields), |
| 42 | structName(other.structName) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 43 | {} |
| 44 | |
| 45 | ShaderVariable &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 Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 53 | fields = other.fields; |
| 54 | structName = other.structName; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 55 | return *this; |
| 56 | } |
| 57 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 58 | bool 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 | |
| 79 | bool 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 Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 89 | |
| 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 | |
| 150 | bool 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 Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 177 | Uniform::Uniform() |
| 178 | {} |
| 179 | |
| 180 | Uniform::~Uniform() |
| 181 | {} |
| 182 | |
| 183 | Uniform::Uniform(const Uniform &other) |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 184 | : ShaderVariable(other) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 185 | {} |
| 186 | |
| 187 | Uniform &Uniform::operator=(const Uniform &other) |
| 188 | { |
| 189 | ShaderVariable::operator=(other); |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 190 | return *this; |
| 191 | } |
| 192 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 193 | bool Uniform::operator==(const Uniform &other) const |
| 194 | { |
| 195 | return ShaderVariable::operator==(other); |
| 196 | } |
| 197 | |
| 198 | bool Uniform::isSameUniformAtLinkTime(const Uniform &other) const |
| 199 | { |
| 200 | return ShaderVariable::isSameVariableAtLinkTime(other, true); |
| 201 | } |
| 202 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 203 | Attribute::Attribute() |
| 204 | : location(-1) |
| 205 | {} |
| 206 | |
| 207 | Attribute::~Attribute() |
| 208 | {} |
| 209 | |
| 210 | Attribute::Attribute(const Attribute &other) |
| 211 | : ShaderVariable(other), |
| 212 | location(other.location) |
| 213 | {} |
| 214 | |
| 215 | Attribute &Attribute::operator=(const Attribute &other) |
| 216 | { |
| 217 | ShaderVariable::operator=(other); |
| 218 | location = other.location; |
| 219 | return *this; |
| 220 | } |
| 221 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 222 | bool Attribute::operator==(const Attribute &other) const |
| 223 | { |
| 224 | return (ShaderVariable::operator==(other) && |
| 225 | location == other.location); |
| 226 | } |
| 227 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 228 | InterfaceBlockField::InterfaceBlockField() |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 229 | : isRowMajorLayout(false) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 230 | {} |
| 231 | |
| 232 | InterfaceBlockField::~InterfaceBlockField() |
| 233 | {} |
| 234 | |
| 235 | InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other) |
| 236 | : ShaderVariable(other), |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 237 | isRowMajorLayout(other.isRowMajorLayout) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 238 | {} |
| 239 | |
| 240 | InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other) |
| 241 | { |
| 242 | ShaderVariable::operator=(other); |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 243 | isRowMajorLayout = other.isRowMajorLayout; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 244 | return *this; |
| 245 | } |
| 246 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 247 | bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const |
| 248 | { |
| 249 | return (ShaderVariable::operator==(other) && |
| 250 | isRowMajorLayout == other.isRowMajorLayout); |
| 251 | } |
| 252 | |
| 253 | bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime( |
| 254 | const InterfaceBlockField &other) const |
| 255 | { |
| 256 | return (ShaderVariable::isSameVariableAtLinkTime(other, true) && |
| 257 | isRowMajorLayout == other.isRowMajorLayout); |
| 258 | } |
| 259 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 260 | Varying::Varying() |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 261 | : interpolation(INTERPOLATION_SMOOTH), |
| 262 | isInvariant(false) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 263 | {} |
| 264 | |
| 265 | Varying::~Varying() |
| 266 | {} |
| 267 | |
| 268 | Varying::Varying(const Varying &other) |
| 269 | : ShaderVariable(other), |
| 270 | interpolation(other.interpolation), |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 271 | isInvariant(other.isInvariant) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 272 | {} |
| 273 | |
| 274 | Varying &Varying::operator=(const Varying &other) |
| 275 | { |
| 276 | ShaderVariable::operator=(other); |
| 277 | interpolation = other.interpolation; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 278 | isInvariant = other.isInvariant; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 279 | return *this; |
| 280 | } |
| 281 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 282 | bool Varying::operator==(const Varying &other) const |
| 283 | { |
| 284 | return (ShaderVariable::operator==(other) && |
| 285 | interpolation == other.interpolation && |
| 286 | isInvariant == other.isInvariant); |
| 287 | } |
| 288 | |
| 289 | bool Varying::isSameVaryingAtLinkTime(const Varying &other) const |
| 290 | { |
| 291 | return (ShaderVariable::isSameVariableAtLinkTime(other, false) && |
| 292 | interpolation == other.interpolation && |
| 293 | isInvariant == other.isInvariant); |
| 294 | } |
| 295 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 296 | InterfaceBlock::InterfaceBlock() |
| 297 | : arraySize(0), |
| 298 | layout(BLOCKLAYOUT_PACKED), |
| 299 | isRowMajorLayout(false), |
| 300 | staticUse(false) |
| 301 | {} |
| 302 | |
| 303 | InterfaceBlock::~InterfaceBlock() |
| 304 | {} |
| 305 | |
| 306 | InterfaceBlock::InterfaceBlock(const InterfaceBlock &other) |
| 307 | : name(other.name), |
| 308 | mappedName(other.mappedName), |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 309 | instanceName(other.instanceName), |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 310 | arraySize(other.arraySize), |
| 311 | layout(other.layout), |
| 312 | isRowMajorLayout(other.isRowMajorLayout), |
| 313 | staticUse(other.staticUse), |
| 314 | fields(other.fields) |
| 315 | {} |
| 316 | |
| 317 | InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other) |
| 318 | { |
| 319 | name = other.name; |
| 320 | mappedName = other.mappedName; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 321 | instanceName = other.instanceName; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 322 | 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 | } |