From 8ce7c6f6958f9f5ed750ef1a823b9e9ed8c042d8 Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 6 Dec 2024 00:55:35 -0800 Subject: Support specialization constant on WGSL and Metal. (#5780) --- docs/user-guide/a2-02-metal-target-specific.md | 19 ++++++++++++++++++- docs/user-guide/a2-03-wgsl-target-specific.md | 18 +++++++++++++++++- docs/user-guide/toc.html | 2 ++ 3 files changed, 37 insertions(+), 2 deletions(-) (limited to 'docs') diff --git a/docs/user-guide/a2-02-metal-target-specific.md b/docs/user-guide/a2-02-metal-target-specific.md index 827f11200..e2602eb77 100644 --- a/docs/user-guide/a2-02-metal-target-specific.md +++ b/docs/user-guide/a2-02-metal-target-specific.md @@ -274,4 +274,21 @@ The HLSL `:register()` semantic is respected when emitting Metal code. Since metal does not differentiate a constant buffer, a shader resource (read-only) buffer and an unordered access buffer, Slang will map `register(tN)`, `register(uN)` and `register(bN)` to `[[buffer(N)]]` when such `register` semantic is declared on a buffer typed parameter. -`spaceN` specifiers inside `register` semantics are ignored. \ No newline at end of file +`spaceN` specifiers inside `register` semantics are ignored. + +## Specialization Constants + +Specialization constants declared with the `[SpecializationConstant]` or `[vk::constant_id]` attribute will be translated into a `function_constant` when generating Metal source. +For example: + +```csharp +[vk::constant_id(7)] +const int a = 2; +``` + +Translates to: + +```metal +constant int fc_a_0 [[function_constant(7)]]; +constant int a_0 = is_function_constant_defined(fc_a_0) ? fc_a_0 : 2; +``` \ No newline at end of file diff --git a/docs/user-guide/a2-03-wgsl-target-specific.md b/docs/user-guide/a2-03-wgsl-target-specific.md index c802a682b..743928a5f 100644 --- a/docs/user-guide/a2-03-wgsl-target-specific.md +++ b/docs/user-guide/a2-03-wgsl-target-specific.md @@ -161,4 +161,20 @@ Since the WGSL matrix multiplication convention is the normal one, where inner p The `[vk::binding(index,set)]` attribute is respected when emitting WGSL code, and will translate to `@binding(index) @group(set)` in WGSL. -If the `[vk::binding()]` attribute is not specified by a `:register()` semantic is present, Slang will derive the binding from the `register` semantic the same way as the SPIRV and GLSL backends. \ No newline at end of file +If the `[vk::binding()]` attribute is not specified by a `:register()` semantic is present, Slang will derive the binding from the `register` semantic the same way as the SPIRV and GLSL backends. + +## Specialization Constants + +Specialization constants declared with the `[SpecializationConstant]` or `[vk::constant_id]` attribute will be translated into a global `override` declaration when generating WGSL source. +For example: + +```csharp +[vk::constant_id(7)] +const int a = 2; +``` + +Translates to: + +```wgsl +@id(7) override a : i32 = 2; +``` \ No newline at end of file diff --git a/docs/user-guide/toc.html b/docs/user-guide/toc.html index 39cfd6164..283706adc 100644 --- a/docs/user-guide/toc.html +++ b/docs/user-guide/toc.html @@ -234,6 +234,7 @@
  • Conservative Rasterization
  • Address Space Assignment
  • Explicit Parameter Binding
  • +
  • Specialization Constants
  • WGSL specific functionalities @@ -251,6 +252,7 @@
  • Address Space Assignment
  • Matrix type translation
  • Explicit Parameter Binding
  • +
  • Specialization Constants
  • -- cgit v1.2.3