ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SKSL_LAYOUT |
| 9 | #define SKSL_LAYOUT |
| 10 | |
| 11 | namespace SkSL { |
| 12 | |
| 13 | /** |
| 14 | * Represents a layout block appearing before a variable declaration, as in: |
| 15 | * |
| 16 | * layout (location = 0) int x; |
| 17 | */ |
| 18 | struct Layout { |
| 19 | Layout(const ASTLayout& layout) |
| 20 | : fLocation(layout.fLocation) |
| 21 | , fBinding(layout.fBinding) |
| 22 | , fIndex(layout.fIndex) |
| 23 | , fSet(layout.fSet) |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 24 | , fBuiltin(layout.fBuiltin) |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 25 | , fOriginUpperLeft(layout.fOriginUpperLeft) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 26 | |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 27 | Layout(int location, int binding, int index, int set, int builtin, bool originUpperLeft) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 28 | : fLocation(location) |
| 29 | , fBinding(binding) |
| 30 | , fIndex(index) |
| 31 | , fSet(set) |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 32 | , fBuiltin(builtin) |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 33 | , fOriginUpperLeft(originUpperLeft) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 34 | |
| 35 | std::string description() const { |
| 36 | std::string result; |
| 37 | std::string separator; |
| 38 | if (fLocation >= 0) { |
| 39 | result += separator + "location = " + to_string(fLocation); |
| 40 | separator = ", "; |
| 41 | } |
| 42 | if (fBinding >= 0) { |
| 43 | result += separator + "binding = " + to_string(fBinding); |
| 44 | separator = ", "; |
| 45 | } |
| 46 | if (fIndex >= 0) { |
| 47 | result += separator + "index = " + to_string(fIndex); |
| 48 | separator = ", "; |
| 49 | } |
| 50 | if (fSet >= 0) { |
| 51 | result += separator + "set = " + to_string(fSet); |
| 52 | separator = ", "; |
| 53 | } |
| 54 | if (fBuiltin >= 0) { |
| 55 | result += separator + "builtin = " + to_string(fBuiltin); |
| 56 | separator = ", "; |
| 57 | } |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 58 | if (fOriginUpperLeft) { |
| 59 | result += separator + "origin_upper_left"; |
| 60 | separator = ", "; |
| 61 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 62 | if (result.length() > 0) { |
| 63 | result = "layout (" + result + ")"; |
| 64 | } |
| 65 | return result; |
| 66 | } |
| 67 | |
| 68 | bool operator==(const Layout& other) const { |
fmalita | d214d6a | 2016-09-30 08:05:24 -0700 | [diff] [blame^] | 69 | return fLocation == other.fLocation && |
| 70 | fBinding == other.fBinding && |
| 71 | fIndex == other.fIndex && |
| 72 | fSet == other.fSet && |
| 73 | fBuiltin == other.fBuiltin; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool operator!=(const Layout& other) const { |
| 77 | return !(*this == other); |
| 78 | } |
| 79 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 80 | // everything but builtin is in the GLSL spec; builtin comes from SPIR-V and identifies which |
| 81 | // particular builtin value this object represents. |
| 82 | int fLocation; |
| 83 | int fBinding; |
| 84 | int fIndex; |
| 85 | int fSet; |
| 86 | int fBuiltin; |
| 87 | bool fOriginUpperLeft; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | #endif |