diff options
| author | Yong He <yonghe@outlook.com> | 2024-07-10 13:27:10 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-10 13:27:10 -0700 |
| commit | 16a47816747ca9a9de67b842a73f0e981dbc8b91 (patch) | |
| tree | 7613f352bafd627ce7f35785f67afa20f85931f8 /docs/user-guide/03-convenience-features.md | |
| parent | ff9437e6c926c1e7c6a0ebe66592b46dbb3fb36b (diff) | |
Add user guide section on user-defined attributes. (#4557)
* Add user guide section on user-defined attributes.
* Update toc
Diffstat (limited to 'docs/user-guide/03-convenience-features.md')
| -rw-r--r-- | docs/user-guide/03-convenience-features.md | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/docs/user-guide/03-convenience-features.md b/docs/user-guide/03-convenience-features.md index a145c407c..f4cd51b3f 100644 --- a/docs/user-guide/03-convenience-features.md +++ b/docs/user-guide/03-convenience-features.md @@ -613,3 +613,28 @@ __file_decl } } ``` + +User Defined Attributes (Experimental) +------------------- + +In addition to many system defined attributes, users can define their own custom attribute types to be used in the `[UserDefinedAttribute(args...)]` syntax. The following example shows how to define a custom attribute type. + +```csharp +[__AttributeUsage(_AttributeTargets.Var)] +struct MaxValueAttribute +{ + int value; + string description; +}; + +[MaxValue(12, "the scale factor")] +uniform int scaleFactor; +``` + +In the above code, the `MaxValueAttribute` struct type is decorated with the `[__AttributeUsage]` attribute, which informs that `MaxValueAttribute` type should be interpreted as a definiton for a user-defined attribute, `[MaxValue]`, that can be used to decorate all variables or fields. The members of the struct defines the argument list for the attribute. + +The `scaleFactor` uniform parameter is declared with the user defined `[MaxValue]` attribute, providing two arguments for `value` and `description`. + +The `_AttributeTargets` enum is used to restrict the type of decls the attribute can apply. Possible values of `_AttributeTargets` can be `Function`, `Param`, `Struct` or `Var`. + +The usage of user-defined attributes can be queried via Slang's reflection API through `TypeReflection` or `VariableReflection`'s `getUserAttributeCount`, `getUserAttributeByIndex` and `findUserAttributeByName` methods. |
