From 2d7106640addf0ac88e0a5462117cd90b13a5e73 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 4 Jun 2025 13:07:11 -0700 Subject: 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` type to core module. * Update toc. * Fix wording. * Update test. --- docs/user-guide/03-convenience-features.md | 60 +++++++++++++++++++++++++++++- docs/user-guide/toc.html | 1 + 2 files changed, 59 insertions(+), 2 deletions(-) (limited to 'docs/user-guide') 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` 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 normal{get;} + property Optional color{get;} +} + +struct Vertex : IVertex +{ + private float3 m_position; + private Conditional m_normal; + private Conditional 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 normal + { + get { return m_normal; } + } + property Optional 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 vertMain(VertexIn inputVertex) +{ + ... +} +``` + + ## `if_let` syntax -Slang supports `if (let name = expr)` syntax to simplify the code when working with `Optional` value. The syntax is similar to Rust's -`if let` syntax, the value expression must be an `Optional` type, for example: +Slang supports `if (let name = expr)` syntax to simplify the code when working with `Optional` or `Conditional` value. The syntax is similar to Rust's +`if let` syntax, the value expression must be an `Optional` or `Conditional` type, for example: ```csharp Optional 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 @@
  • Subscript Operator
  • Tuple Types
  • `Optional<T>` type
  • +
  • `Conditional<T, bool condition>` Type
  • `if_let` syntax
  • `reinterpret<T>` operation
  • Pointers (limited)
  • -- cgit v1.2.3