Skip to content

Commit 16a4781

Browse files
authored
Add user guide section on user-defined attributes. (shader-slang#4557)
* Add user guide section on user-defined attributes. * Update toc
1 parent ff9437e commit 16a4781

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

docs/user-guide/03-convenience-features.md

+25
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,28 @@ __file_decl
613613
}
614614
}
615615
```
616+
617+
User Defined Attributes (Experimental)
618+
-------------------
619+
620+
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.
621+
622+
```csharp
623+
[__AttributeUsage(_AttributeTargets.Var)]
624+
struct MaxValueAttribute
625+
{
626+
int value;
627+
string description;
628+
};
629+
630+
[MaxValue(12, "the scale factor")]
631+
uniform int scaleFactor;
632+
```
633+
634+
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.
635+
636+
The `scaleFactor` uniform parameter is declared with the user defined `[MaxValue]` attribute, providing two arguments for `value` and `description`.
637+
638+
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`.
639+
640+
The usage of user-defined attributes can be queried via Slang's reflection API through `TypeReflection` or `VariableReflection`'s `getUserAttributeCount`, `getUserAttributeByIndex` and `findUserAttributeByName` methods.

docs/user-guide/toc.html

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<li data-link="convenience-features#multi-level-break"><span>Multi-level break</span></li>
4747
<li data-link="convenience-features#force-inlining"><span>Force inlining</span></li>
4848
<li data-link="convenience-features#special-scoping-syntax"><span>Special Scoping Syntax</span></li>
49+
<li data-link="convenience-features#user-defined-attributes-experimental"><span>User Defined Attributes (Experimental)</span></li>
4950
</ul>
5051
</li>
5152
<li data-link="modules"><span>Modules and Access Control</span>
@@ -150,8 +151,8 @@
150151
</li>
151152
<li data-link="a1-02-slangpy"><span>Using Slang to Write PyTorch Kernels</span>
152153
<ul class="toc_list">
153-
<li data-link="a1-02-slangpy#getting-started-with-slangpy"><span>Getting Started with slangpy</span></li>
154-
<li data-link="a1-02-slangpy#specializing-shaders-using-slangpy"><span>Specializing shaders using slangpy</span></li>
154+
<li data-link="a1-02-slangpy#getting-started-with-slangtorch"><span>Getting Started with SlangTorch</span></li>
155+
<li data-link="a1-02-slangpy#specializing-shaders-using-slangtorch"><span>Specializing shaders using slangtorch</span></li>
155156
<li data-link="a1-02-slangpy#back-propagating-derivatives-through-complex-access-patterns"><span>Back-propagating Derivatives through Complex Access Patterns</span></li>
156157
<li data-link="a1-02-slangpy#manually-binding-kernels"><span>Manually binding kernels</span></li>
157158
<li data-link="a1-02-slangpy#builtin-library-support-for-pytorch-interop"><span>Builtin Library Support for PyTorch Interop</span></li>
@@ -187,4 +188,4 @@
187188
</li>
188189
</ul>
189190
</li>
190-
</ul>
191+
</ul>

0 commit comments

Comments
 (0)