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(".["); |
| 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 | |
| 151 | bool 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 Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 178 | Uniform::Uniform() |
| 179 | {} |
| 180 | |
| 181 | Uniform::~Uniform() |
| 182 | {} |
| 183 | |
| 184 | Uniform::Uniform(const Uniform &other) |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 185 | : ShaderVariable(other) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 186 | {} |
| 187 | |
| 188 | Uniform &Uniform::operator=(const Uniform &other) |
| 189 | { |
| 190 | ShaderVariable::operator=(other); |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 191 | return *this; |
| 192 | } |
| 193 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame^] | 194 | bool Uniform::operator==(const Uniform &other) const |
| 195 | { |
| 196 | return ShaderVariable::operator==(other); |
| 197 | } |
| 198 | |
| 199 | bool Uniform::isSameUniformAtLinkTime(const Uniform &other) const |
| 200 | { |
| 201 | return ShaderVariable::isSameVariableAtLinkTime(other, true); |
| 202 | } |
| 203 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 204 | Attribute::Attribute() |
| 205 | : location(-1) |
| 206 | {} |
| 207 | |
| 208 | Attribute::~Attribute() |
| 209 | {} |
| 210 | |
| 211 | Attribute::Attribute(const Attribute &other) |
| 212 | : ShaderVariable(other), |
| 213 | location(other.location) |
| 214 | {} |
| 215 | |
| 216 | Attribute &Attribute::operator=(const Attribute &other) |
| 217 | { |
| 218 | ShaderVariable::operator=(other); |
| 219 | location = other.location; |
| 220 | return *this; |
| 221 | } |
| 222 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame^] | 223 | bool Attribute::operator==(const Attribute &other) const |
| 224 | { |
| 225 | return (ShaderVariable::operator==(other) && |
| 226 | location == other.location); |
| 227 | } |
| 228 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 229 | InterfaceBlockField::InterfaceBlockField() |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 230 | : isRowMajorLayout(false) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 231 | {} |
| 232 | |
| 233 | InterfaceBlockField::~InterfaceBlockField() |
| 234 | {} |
| 235 | |
| 236 | InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other) |
| 237 | : ShaderVariable(other), |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 238 | isRowMajorLayout(other.isRowMajorLayout) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 239 | {} |
| 240 | |
| 241 | InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other) |
| 242 | { |
| 243 | ShaderVariable::operator=(other); |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 244 | isRowMajorLayout = other.isRowMajorLayout; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 245 | return *this; |
| 246 | } |
| 247 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame^] | 248 | bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const |
| 249 | { |
| 250 | return (ShaderVariable::operator==(other) && |
| 251 | isRowMajorLayout == other.isRowMajorLayout); |
| 252 | } |
| 253 | |
| 254 | bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime( |
| 255 | const InterfaceBlockField &other) const |
| 256 | { |
| 257 | return (ShaderVariable::isSameVariableAtLinkTime(other, true) && |
| 258 | isRowMajorLayout == other.isRowMajorLayout); |
| 259 | } |
| 260 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 261 | Varying::Varying() |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 262 | : interpolation(INTERPOLATION_SMOOTH), |
| 263 | isInvariant(false) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 264 | {} |
| 265 | |
| 266 | Varying::~Varying() |
| 267 | {} |
| 268 | |
| 269 | Varying::Varying(const Varying &other) |
| 270 | : ShaderVariable(other), |
| 271 | interpolation(other.interpolation), |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 272 | isInvariant(other.isInvariant) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 273 | {} |
| 274 | |
| 275 | Varying &Varying::operator=(const Varying &other) |
| 276 | { |
| 277 | ShaderVariable::operator=(other); |
| 278 | interpolation = other.interpolation; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 279 | isInvariant = other.isInvariant; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 280 | return *this; |
| 281 | } |
| 282 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame^] | 283 | bool Varying::operator==(const Varying &other) const |
| 284 | { |
| 285 | return (ShaderVariable::operator==(other) && |
| 286 | interpolation == other.interpolation && |
| 287 | isInvariant == other.isInvariant); |
| 288 | } |
| 289 | |
| 290 | bool Varying::isSameVaryingAtLinkTime(const Varying &other) const |
| 291 | { |
| 292 | return (ShaderVariable::isSameVariableAtLinkTime(other, false) && |
| 293 | interpolation == other.interpolation && |
| 294 | isInvariant == other.isInvariant); |
| 295 | } |
| 296 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 297 | InterfaceBlock::InterfaceBlock() |
| 298 | : arraySize(0), |
| 299 | layout(BLOCKLAYOUT_PACKED), |
| 300 | isRowMajorLayout(false), |
| 301 | staticUse(false) |
| 302 | {} |
| 303 | |
| 304 | InterfaceBlock::~InterfaceBlock() |
| 305 | {} |
| 306 | |
| 307 | InterfaceBlock::InterfaceBlock(const InterfaceBlock &other) |
| 308 | : name(other.name), |
| 309 | mappedName(other.mappedName), |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 310 | instanceName(other.instanceName), |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 311 | arraySize(other.arraySize), |
| 312 | layout(other.layout), |
| 313 | isRowMajorLayout(other.isRowMajorLayout), |
| 314 | staticUse(other.staticUse), |
| 315 | fields(other.fields) |
| 316 | {} |
| 317 | |
| 318 | InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other) |
| 319 | { |
| 320 | name = other.name; |
| 321 | mappedName = other.mappedName; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 322 | instanceName = other.instanceName; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 323 | 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 | } |