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 | |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 12 | #include "common/debug.h" |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 13 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 14 | namespace sh |
| 15 | { |
| 16 | |
Jamie Madill | e9cc469 | 2015-02-19 16:00:13 -0500 | [diff] [blame] | 17 | namespace |
| 18 | { |
| 19 | |
| 20 | InterpolationType GetNonAuxiliaryInterpolationType(InterpolationType interpolation) |
| 21 | { |
| 22 | return (interpolation == INTERPOLATION_CENTROID ? INTERPOLATION_SMOOTH : interpolation); |
| 23 | } |
| 24 | |
| 25 | } |
| 26 | // The ES 3.0 spec is not clear on this point, but the ES 3.1 spec, and discussion |
| 27 | // on Khronos.org, clarifies that a smooth/flat mismatch produces a link error, |
| 28 | // but auxiliary qualifier mismatch (centroid) does not. |
| 29 | bool InterpolationTypesMatch(InterpolationType a, InterpolationType b) |
| 30 | { |
| 31 | return (GetNonAuxiliaryInterpolationType(a) == GetNonAuxiliaryInterpolationType(b)); |
| 32 | } |
| 33 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 34 | ShaderVariable::ShaderVariable() |
| 35 | : type(0), |
| 36 | precision(0), |
| 37 | arraySize(0), |
| 38 | staticUse(false) |
| 39 | {} |
| 40 | |
| 41 | ShaderVariable::ShaderVariable(GLenum typeIn, unsigned int arraySizeIn) |
| 42 | : type(typeIn), |
| 43 | precision(0), |
| 44 | arraySize(arraySizeIn), |
| 45 | staticUse(false) |
| 46 | {} |
| 47 | |
| 48 | ShaderVariable::~ShaderVariable() |
| 49 | {} |
| 50 | |
| 51 | ShaderVariable::ShaderVariable(const ShaderVariable &other) |
| 52 | : type(other.type), |
| 53 | precision(other.precision), |
| 54 | name(other.name), |
| 55 | mappedName(other.mappedName), |
| 56 | arraySize(other.arraySize), |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 57 | staticUse(other.staticUse), |
| 58 | fields(other.fields), |
| 59 | structName(other.structName) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 60 | {} |
| 61 | |
| 62 | ShaderVariable &ShaderVariable::operator=(const ShaderVariable &other) |
| 63 | { |
| 64 | type = other.type; |
| 65 | precision = other.precision; |
| 66 | name = other.name; |
| 67 | mappedName = other.mappedName; |
| 68 | arraySize = other.arraySize; |
| 69 | staticUse = other.staticUse; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 70 | fields = other.fields; |
| 71 | structName = other.structName; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 72 | return *this; |
| 73 | } |
| 74 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 75 | bool ShaderVariable::operator==(const ShaderVariable &other) const |
| 76 | { |
| 77 | if (type != other.type || |
| 78 | precision != other.precision || |
| 79 | name != other.name || |
| 80 | mappedName != other.mappedName || |
| 81 | arraySize != other.arraySize || |
| 82 | staticUse != other.staticUse || |
| 83 | fields.size() != other.fields.size() || |
| 84 | structName != other.structName) |
| 85 | { |
| 86 | return false; |
| 87 | } |
| 88 | for (size_t ii = 0; ii < fields.size(); ++ii) |
| 89 | { |
| 90 | if (fields[ii] != other.fields[ii]) |
| 91 | return false; |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool ShaderVariable::findInfoByMappedName( |
| 97 | const std::string &mappedFullName, |
| 98 | const ShaderVariable **leafVar, std::string *originalFullName) const |
| 99 | { |
| 100 | ASSERT(leafVar && originalFullName); |
| 101 | // There are three cases: |
| 102 | // 1) the top variable is of struct type; |
| 103 | // 2) the top variable is an array; |
| 104 | // 3) otherwise. |
| 105 | size_t pos = mappedFullName.find_first_of(".["); |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 106 | |
| 107 | if (pos == std::string::npos) |
| 108 | { |
| 109 | // Case 3. |
| 110 | if (mappedFullName != this->mappedName) |
| 111 | return false; |
| 112 | *originalFullName = this->name; |
| 113 | *leafVar = this; |
| 114 | return true; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | std::string topName = mappedFullName.substr(0, pos); |
| 119 | if (topName != this->mappedName) |
| 120 | return false; |
| 121 | std::string originalName = this->name; |
| 122 | std::string remaining; |
| 123 | if (mappedFullName[pos] == '[') |
| 124 | { |
| 125 | // Case 2. |
| 126 | size_t closePos = mappedFullName.find_first_of(']'); |
| 127 | if (closePos < pos || closePos == std::string::npos) |
| 128 | return false; |
| 129 | // Append '[index]'. |
| 130 | originalName += mappedFullName.substr(pos, closePos - pos + 1); |
| 131 | if (closePos + 1 == mappedFullName.size()) |
| 132 | { |
| 133 | *originalFullName = originalName; |
| 134 | *leafVar = this; |
| 135 | return true; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | // In the form of 'a[0].b', so after ']', '.' is expected. |
| 140 | if (mappedFullName[closePos + 1] != '.') |
| 141 | return false; |
| 142 | remaining = mappedFullName.substr(closePos + 2); // Skip "]." |
| 143 | } |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | // Case 1. |
| 148 | remaining = mappedFullName.substr(pos + 1); // Skip "." |
| 149 | } |
| 150 | for (size_t ii = 0; ii < this->fields.size(); ++ii) |
| 151 | { |
| 152 | const ShaderVariable *fieldVar = NULL; |
| 153 | std::string originalFieldName; |
| 154 | bool found = fields[ii].findInfoByMappedName( |
| 155 | remaining, &fieldVar, &originalFieldName); |
| 156 | if (found) |
| 157 | { |
| 158 | *originalFullName = originalName + "." + originalFieldName; |
| 159 | *leafVar = fieldVar; |
| 160 | return true; |
| 161 | } |
| 162 | } |
| 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | bool ShaderVariable::isSameVariableAtLinkTime( |
| 168 | const ShaderVariable &other, bool matchPrecision) const |
| 169 | { |
| 170 | if (type != other.type) |
| 171 | return false; |
| 172 | if (matchPrecision && precision != other.precision) |
| 173 | return false; |
| 174 | if (name != other.name) |
| 175 | return false; |
| 176 | ASSERT(mappedName == other.mappedName); |
| 177 | if (arraySize != other.arraySize) |
| 178 | return false; |
| 179 | if (fields.size() != other.fields.size()) |
| 180 | return false; |
| 181 | for (size_t ii = 0; ii < fields.size(); ++ii) |
| 182 | { |
| 183 | if (!fields[ii].isSameVariableAtLinkTime(other.fields[ii], |
| 184 | matchPrecision)) |
| 185 | { |
| 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | if (structName != other.structName) |
| 190 | return false; |
| 191 | return true; |
| 192 | } |
| 193 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 194 | Uniform::Uniform() |
| 195 | {} |
| 196 | |
| 197 | Uniform::~Uniform() |
| 198 | {} |
| 199 | |
| 200 | Uniform::Uniform(const Uniform &other) |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 201 | : ShaderVariable(other) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 202 | {} |
| 203 | |
| 204 | Uniform &Uniform::operator=(const Uniform &other) |
| 205 | { |
| 206 | ShaderVariable::operator=(other); |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 207 | return *this; |
| 208 | } |
| 209 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 210 | bool Uniform::operator==(const Uniform &other) const |
| 211 | { |
| 212 | return ShaderVariable::operator==(other); |
| 213 | } |
| 214 | |
| 215 | bool Uniform::isSameUniformAtLinkTime(const Uniform &other) const |
| 216 | { |
| 217 | return ShaderVariable::isSameVariableAtLinkTime(other, true); |
| 218 | } |
| 219 | |
Jamie Madill | a0a9e12 | 2015-09-02 15:54:30 -0400 | [diff] [blame] | 220 | InterfaceVariable::InterfaceVariable() : location(-1) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 221 | {} |
| 222 | |
Jamie Madill | a0a9e12 | 2015-09-02 15:54:30 -0400 | [diff] [blame] | 223 | InterfaceVariable::~InterfaceVariable() |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 224 | {} |
| 225 | |
Jamie Madill | a0a9e12 | 2015-09-02 15:54:30 -0400 | [diff] [blame] | 226 | InterfaceVariable::InterfaceVariable(const InterfaceVariable &other) |
| 227 | : ShaderVariable(other), location(other.location) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 228 | {} |
| 229 | |
Jamie Madill | a0a9e12 | 2015-09-02 15:54:30 -0400 | [diff] [blame] | 230 | InterfaceVariable &InterfaceVariable::operator=(const InterfaceVariable &other) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 231 | { |
| 232 | ShaderVariable::operator=(other); |
| 233 | location = other.location; |
| 234 | return *this; |
| 235 | } |
| 236 | |
Jamie Madill | a0a9e12 | 2015-09-02 15:54:30 -0400 | [diff] [blame] | 237 | bool InterfaceVariable::operator==(const InterfaceVariable &other) const |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 238 | { |
| 239 | return (ShaderVariable::operator==(other) && |
| 240 | location == other.location); |
| 241 | } |
| 242 | |
Jamie Madill | a0a9e12 | 2015-09-02 15:54:30 -0400 | [diff] [blame] | 243 | Attribute::Attribute() |
| 244 | { |
| 245 | } |
| 246 | |
| 247 | Attribute::~Attribute() |
| 248 | { |
| 249 | } |
| 250 | |
| 251 | Attribute::Attribute(const Attribute &other) : InterfaceVariable(other) |
| 252 | { |
| 253 | } |
| 254 | |
| 255 | Attribute &Attribute::operator=(const Attribute &other) |
| 256 | { |
| 257 | InterfaceVariable::operator=(other); |
| 258 | return *this; |
| 259 | } |
| 260 | |
| 261 | bool Attribute::operator==(const Attribute &other) const |
| 262 | { |
| 263 | return InterfaceVariable::operator==(other); |
| 264 | } |
| 265 | |
| 266 | OutputVariable::OutputVariable() |
| 267 | { |
| 268 | } |
| 269 | |
| 270 | OutputVariable::~OutputVariable() |
| 271 | { |
| 272 | } |
| 273 | |
| 274 | OutputVariable::OutputVariable(const OutputVariable &other) : InterfaceVariable(other) |
| 275 | { |
| 276 | } |
| 277 | |
| 278 | OutputVariable &OutputVariable::operator=(const OutputVariable &other) |
| 279 | { |
| 280 | InterfaceVariable::operator=(other); |
| 281 | return *this; |
| 282 | } |
| 283 | |
| 284 | bool OutputVariable::operator==(const OutputVariable &other) const |
| 285 | { |
| 286 | return InterfaceVariable::operator==(other); |
| 287 | } |
| 288 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 289 | InterfaceBlockField::InterfaceBlockField() |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 290 | : isRowMajorLayout(false) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 291 | {} |
| 292 | |
| 293 | InterfaceBlockField::~InterfaceBlockField() |
| 294 | {} |
| 295 | |
| 296 | InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other) |
| 297 | : ShaderVariable(other), |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 298 | isRowMajorLayout(other.isRowMajorLayout) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 299 | {} |
| 300 | |
| 301 | InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other) |
| 302 | { |
| 303 | ShaderVariable::operator=(other); |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 304 | isRowMajorLayout = other.isRowMajorLayout; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 305 | return *this; |
| 306 | } |
| 307 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 308 | bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const |
| 309 | { |
| 310 | return (ShaderVariable::operator==(other) && |
| 311 | isRowMajorLayout == other.isRowMajorLayout); |
| 312 | } |
| 313 | |
| 314 | bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime( |
| 315 | const InterfaceBlockField &other) const |
| 316 | { |
| 317 | return (ShaderVariable::isSameVariableAtLinkTime(other, true) && |
| 318 | isRowMajorLayout == other.isRowMajorLayout); |
| 319 | } |
| 320 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 321 | Varying::Varying() |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 322 | : interpolation(INTERPOLATION_SMOOTH), |
| 323 | isInvariant(false) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 324 | {} |
| 325 | |
| 326 | Varying::~Varying() |
| 327 | {} |
| 328 | |
| 329 | Varying::Varying(const Varying &other) |
| 330 | : ShaderVariable(other), |
| 331 | interpolation(other.interpolation), |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 332 | isInvariant(other.isInvariant) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 333 | {} |
| 334 | |
| 335 | Varying &Varying::operator=(const Varying &other) |
| 336 | { |
| 337 | ShaderVariable::operator=(other); |
| 338 | interpolation = other.interpolation; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 339 | isInvariant = other.isInvariant; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 340 | return *this; |
| 341 | } |
| 342 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 343 | bool Varying::operator==(const Varying &other) const |
| 344 | { |
| 345 | return (ShaderVariable::operator==(other) && |
| 346 | interpolation == other.interpolation && |
| 347 | isInvariant == other.isInvariant); |
| 348 | } |
| 349 | |
Jamie Madill | 9e64edc | 2015-05-07 14:08:06 +0000 | [diff] [blame] | 350 | bool Varying::isSameVaryingAtLinkTime(const Varying &other) const |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 351 | { |
Olli Etuaho | 37ad474 | 2015-04-27 13:18:50 +0300 | [diff] [blame] | 352 | return isSameVaryingAtLinkTime(other, 100); |
| 353 | } |
| 354 | |
| 355 | bool Varying::isSameVaryingAtLinkTime(const Varying &other, int shaderVersion) const |
| 356 | { |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 357 | return (ShaderVariable::isSameVariableAtLinkTime(other, false) && |
| 358 | interpolation == other.interpolation && |
Olli Etuaho | 37ad474 | 2015-04-27 13:18:50 +0300 | [diff] [blame] | 359 | (shaderVersion >= 300 || isInvariant == other.isInvariant)); |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 362 | InterfaceBlock::InterfaceBlock() |
| 363 | : arraySize(0), |
| 364 | layout(BLOCKLAYOUT_PACKED), |
| 365 | isRowMajorLayout(false), |
| 366 | staticUse(false) |
| 367 | {} |
| 368 | |
| 369 | InterfaceBlock::~InterfaceBlock() |
| 370 | {} |
| 371 | |
| 372 | InterfaceBlock::InterfaceBlock(const InterfaceBlock &other) |
| 373 | : name(other.name), |
| 374 | mappedName(other.mappedName), |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 375 | instanceName(other.instanceName), |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 376 | arraySize(other.arraySize), |
| 377 | layout(other.layout), |
| 378 | isRowMajorLayout(other.isRowMajorLayout), |
| 379 | staticUse(other.staticUse), |
| 380 | fields(other.fields) |
| 381 | {} |
| 382 | |
| 383 | InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other) |
| 384 | { |
| 385 | name = other.name; |
| 386 | mappedName = other.mappedName; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 387 | instanceName = other.instanceName; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 388 | arraySize = other.arraySize; |
| 389 | layout = other.layout; |
| 390 | isRowMajorLayout = other.isRowMajorLayout; |
| 391 | staticUse = other.staticUse; |
| 392 | fields = other.fields; |
| 393 | return *this; |
| 394 | } |
| 395 | |
Jamie Madill | 3904616 | 2016-02-08 15:05:17 -0500 | [diff] [blame^] | 396 | std::string InterfaceBlock::fieldPrefix() const |
| 397 | { |
| 398 | return instanceName.empty() ? "" : name; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 399 | } |
Jamie Madill | 3904616 | 2016-02-08 15:05:17 -0500 | [diff] [blame^] | 400 | |
| 401 | } // namespace sh |