Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef SRC_TINT_AST_STRUCT_MEMBER_OFFSET_ATTRIBUTE_H_ |
| 16 | #define SRC_TINT_AST_STRUCT_MEMBER_OFFSET_ATTRIBUTE_H_ |
| 17 | |
| 18 | #include <string> |
| 19 | |
| 20 | #include "src/tint/ast/attribute.h" |
dan sinclair | 93df967 | 2022-09-09 14:49:09 +0000 | [diff] [blame] | 21 | #include "src/tint/ast/expression.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 22 | |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 23 | namespace tint::ast { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 24 | |
| 25 | /// A struct member offset attribute |
| 26 | /// @note The WGSL spec removed the `@offset(n)` attribute for `@size(n)` |
| 27 | /// and `@align(n)` in https://github.com/gpuweb/gpuweb/pull/1447. However |
| 28 | /// this attribute is kept because the SPIR-V reader has to deal with absolute |
| 29 | /// offsets, and transforming these to size / align is complex and can be done |
| 30 | /// in a number of ways. The Resolver is responsible for consuming the size and |
| 31 | /// align attributes and transforming these into absolute offsets. It is |
| 32 | /// trivial for the Resolver to handle `@offset(n)` or `@size(n)` / |
| 33 | /// `@align(n)` attributes, so this is what we do, keeping all the layout |
| 34 | /// logic in one place. |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 35 | class StructMemberOffsetAttribute final : public Castable<StructMemberOffsetAttribute, Attribute> { |
| 36 | public: |
| 37 | /// constructor |
| 38 | /// @param pid the identifier of the program that owns this node |
Ben Clayton | 4a92a3c | 2022-07-18 20:50:02 +0000 | [diff] [blame] | 39 | /// @param nid the unique node identifier |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | /// @param src the source of this node |
dan sinclair | 93df967 | 2022-09-09 14:49:09 +0000 | [diff] [blame] | 41 | /// @param expr the offset expression |
| 42 | StructMemberOffsetAttribute(ProgramID pid, |
| 43 | NodeID nid, |
| 44 | const Source& src, |
| 45 | const ast::Expression* expr); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 46 | ~StructMemberOffsetAttribute() override; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 47 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 48 | /// @returns the WGSL name for the attribute |
| 49 | std::string Name() const override; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 50 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 51 | /// Clones this node and all transitive child nodes using the `CloneContext` |
| 52 | /// `ctx`. |
| 53 | /// @param ctx the clone context |
| 54 | /// @return the newly cloned node |
| 55 | const StructMemberOffsetAttribute* Clone(CloneContext* ctx) const override; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 56 | |
dan sinclair | 93df967 | 2022-09-09 14:49:09 +0000 | [diff] [blame] | 57 | /// The offset expression |
| 58 | const ast::Expression* const expr; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
dan sinclair | 34323ac | 2022-04-07 18:39:35 +0000 | [diff] [blame] | 61 | } // namespace tint::ast |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 62 | |
| 63 | #endif // SRC_TINT_AST_STRUCT_MEMBER_OFFSET_ATTRIBUTE_H_ |