summaryrefslogtreecommitdiffstats
path: root/docs/user-guide
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2025-06-04 13:07:11 -0700
committerGitHub <noreply@github.com>2025-06-04 13:07:11 -0700
commit2d7106640addf0ac88e0a5462117cd90b13a5e73 (patch)
tree6ce27db4d6c5bf06db84dee755a82e09b1b3a2ca /docs/user-guide
parent812e478989e27983b8dea7ab11964de751654ba2 (diff)
Add legalization for 0-sized arrays. (#7327)
* Add legalization for 0-sized arrays. * Allow 0-sized arrays in the front-end. * More tests. * Add `Conditional<T, hasValue>` type to core module. * Update toc. * Fix wording. * Update test.
Diffstat (limited to 'docs/user-guide')
-rw-r--r--docs/user-guide/03-convenience-features.md60
-rw-r--r--docs/user-guide/toc.html1
2 files changed, 59 insertions, 2 deletions
diff --git a/docs/user-guide/03-convenience-features.md b/docs/user-guide/03-convenience-features.md
index f49fdce90..3f659015b 100644
--- a/docs/user-guide/03-convenience-features.md
+++ b/docs/user-guide/03-convenience-features.md
@@ -446,9 +446,65 @@ int caller()
}
```
+## `Conditional<T, bool condition>` Type
+
+A `Conditional` type can be used to define struct fields that can be specialized away. If `condition` is `false`, the field will be removed
+by the compiler from the target code. This is useful for scenarios where a developer would like to make sure a field is not defined in a
+specialized shader variant when it is not used by the shader.
+
+For example, a common use case is to define the vertex shader output / fragment shader input:
+
+```slang
+interface IVertex
+{
+ property float3 position{get;}
+ property Optional<float3> normal{get;}
+ property Optional<float3> color{get;}
+}
+
+struct Vertex<bool hasNormal, bool hasColor> : IVertex
+{
+ private float3 m_position;
+ private Conditional<float3, hasNormal> m_normal;
+ private Conditional<float3, hasColor> m_color;
+
+ __init(float3 position, float3 normal, float3 color)
+ {
+ m_position = position;
+ m_normal = normal;
+ m_color = color;
+ }
+
+ property float3 position
+ {
+ get { return m_position; }
+ }
+ property Optional<float3> normal
+ {
+ get { return m_normal; }
+ }
+ property Optional<float3> color
+ {
+ get { return m_color; }
+ }
+}
+```
+
+In this example, `Vertex` type is parameterized on `hasNormal` and `hasColor`. If `hasNormal` is false, the `m_normal` field will be eliminated in the target code, allowing a specialized vertex shader to declare minimum output fields. For example, a vertex shader
+can be defined as follows:
+
+```slang
+[shader("vertex")]
+Vertex<hasNormal, hasColor> vertMain<bool hasNormal, bool hasColor>(VertexIn inputVertex)
+{
+ ...
+}
+```
+
+
## `if_let` syntax
-Slang supports `if (let name = expr)` syntax to simplify the code when working with `Optional<T>` value. The syntax is similar to Rust's
-`if let` syntax, the value expression must be an `Optional<T>` type, for example:
+Slang supports `if (let name = expr)` syntax to simplify the code when working with `Optional<T>` or `Conditional<T, hasValue>` value. The syntax is similar to Rust's
+`if let` syntax, the value expression must be an `Optional<T>` or `Conditional<T, hasValue>` type, for example:
```csharp
Optional<int> getOptInt() { ... }
diff --git a/docs/user-guide/toc.html b/docs/user-guide/toc.html
index 5e8ecc207..1621d1da5 100644
--- a/docs/user-guide/toc.html
+++ b/docs/user-guide/toc.html
@@ -42,6 +42,7 @@
<li data-link="convenience-features#subscript-operator"><span>Subscript Operator</span></li>
<li data-link="convenience-features#tuple-types"><span>Tuple Types</span></li>
<li data-link="convenience-features#optionalt-type"><span>`Optional&lt;T&gt;` type</span></li>
+<li data-link="convenience-features#conditionalt-bool-condition-type"><span>`Conditional&lt;T, bool condition&gt;` Type</span></li>
<li data-link="convenience-features#if_let-syntax"><span>`if_let` syntax</span></li>
<li data-link="convenience-features#reinterprett-operation"><span>`reinterpret&lt;T&gt;` operation</span></li>
<li data-link="convenience-features#pointers-limited"><span>Pointers (limited)</span></li>