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 | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 220 | Attribute::Attribute() |
| 221 | : location(-1) |
| 222 | {} |
| 223 | |
| 224 | Attribute::~Attribute() |
| 225 | {} |
| 226 | |
| 227 | Attribute::Attribute(const Attribute &other) |
| 228 | : ShaderVariable(other), |
| 229 | location(other.location) |
| 230 | {} |
| 231 | |
| 232 | Attribute &Attribute::operator=(const Attribute &other) |
| 233 | { |
| 234 | ShaderVariable::operator=(other); |
| 235 | location = other.location; |
| 236 | return *this; |
| 237 | } |
| 238 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 239 | bool Attribute::operator==(const Attribute &other) const |
| 240 | { |
| 241 | return (ShaderVariable::operator==(other) && |
| 242 | location == other.location); |
| 243 | } |
| 244 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 245 | InterfaceBlockField::InterfaceBlockField() |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 246 | : isRowMajorLayout(false) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 247 | {} |
| 248 | |
| 249 | InterfaceBlockField::~InterfaceBlockField() |
| 250 | {} |
| 251 | |
| 252 | InterfaceBlockField::InterfaceBlockField(const InterfaceBlockField &other) |
| 253 | : ShaderVariable(other), |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 254 | isRowMajorLayout(other.isRowMajorLayout) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 255 | {} |
| 256 | |
| 257 | InterfaceBlockField &InterfaceBlockField::operator=(const InterfaceBlockField &other) |
| 258 | { |
| 259 | ShaderVariable::operator=(other); |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 260 | isRowMajorLayout = other.isRowMajorLayout; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 261 | return *this; |
| 262 | } |
| 263 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 264 | bool InterfaceBlockField::operator==(const InterfaceBlockField &other) const |
| 265 | { |
| 266 | return (ShaderVariable::operator==(other) && |
| 267 | isRowMajorLayout == other.isRowMajorLayout); |
| 268 | } |
| 269 | |
| 270 | bool InterfaceBlockField::isSameInterfaceBlockFieldAtLinkTime( |
| 271 | const InterfaceBlockField &other) const |
| 272 | { |
| 273 | return (ShaderVariable::isSameVariableAtLinkTime(other, true) && |
| 274 | isRowMajorLayout == other.isRowMajorLayout); |
| 275 | } |
| 276 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 277 | Varying::Varying() |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 278 | : interpolation(INTERPOLATION_SMOOTH), |
| 279 | isInvariant(false) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 280 | {} |
| 281 | |
| 282 | Varying::~Varying() |
| 283 | {} |
| 284 | |
| 285 | Varying::Varying(const Varying &other) |
| 286 | : ShaderVariable(other), |
| 287 | interpolation(other.interpolation), |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 288 | isInvariant(other.isInvariant) |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 289 | {} |
| 290 | |
| 291 | Varying &Varying::operator=(const Varying &other) |
| 292 | { |
| 293 | ShaderVariable::operator=(other); |
| 294 | interpolation = other.interpolation; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 295 | isInvariant = other.isInvariant; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 296 | return *this; |
| 297 | } |
| 298 | |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 299 | bool Varying::operator==(const Varying &other) const |
| 300 | { |
| 301 | return (ShaderVariable::operator==(other) && |
| 302 | interpolation == other.interpolation && |
| 303 | isInvariant == other.isInvariant); |
| 304 | } |
| 305 | |
Jamie Madill | 9e64edc | 2015-05-07 14:08:06 +0000 | [diff] [blame^] | 306 | bool Varying::isSameVaryingAtLinkTime(const Varying &other) const |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 307 | { |
| 308 | return (ShaderVariable::isSameVariableAtLinkTime(other, false) && |
| 309 | interpolation == other.interpolation && |
Jamie Madill | 9e64edc | 2015-05-07 14:08:06 +0000 | [diff] [blame^] | 310 | isInvariant == other.isInvariant); |
Zhenyao Mo | ed13636 | 2014-10-03 13:23:01 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 313 | InterfaceBlock::InterfaceBlock() |
| 314 | : arraySize(0), |
| 315 | layout(BLOCKLAYOUT_PACKED), |
| 316 | isRowMajorLayout(false), |
| 317 | staticUse(false) |
| 318 | {} |
| 319 | |
| 320 | InterfaceBlock::~InterfaceBlock() |
| 321 | {} |
| 322 | |
| 323 | InterfaceBlock::InterfaceBlock(const InterfaceBlock &other) |
| 324 | : name(other.name), |
| 325 | mappedName(other.mappedName), |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 326 | instanceName(other.instanceName), |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 327 | arraySize(other.arraySize), |
| 328 | layout(other.layout), |
| 329 | isRowMajorLayout(other.isRowMajorLayout), |
| 330 | staticUse(other.staticUse), |
| 331 | fields(other.fields) |
| 332 | {} |
| 333 | |
| 334 | InterfaceBlock &InterfaceBlock::operator=(const InterfaceBlock &other) |
| 335 | { |
| 336 | name = other.name; |
| 337 | mappedName = other.mappedName; |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 338 | instanceName = other.instanceName; |
Jamie Madill | 6a72979 | 2014-07-18 10:33:14 -0400 | [diff] [blame] | 339 | arraySize = other.arraySize; |
| 340 | layout = other.layout; |
| 341 | isRowMajorLayout = other.isRowMajorLayout; |
| 342 | staticUse = other.staticUse; |
| 343 | fields = other.fields; |
| 344 | return *this; |
| 345 | } |
| 346 | |
| 347 | } |