diff options
| author | Yong He <yonghe@outlook.com> | 2024-07-26 19:42:15 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-26 19:42:15 -0700 |
| commit | 7e2bc8e06f61d554bae9bbebc1db0302eb3f1d8a (patch) | |
| tree | 0f10e4a45cb81af2908da61743a4518de27748e2 /docs/user-guide/02-conventional-features.md | |
| parent | c0bff66541302309ff4833e8d4ae2eba1561498a (diff) | |
Allow passing sized array to unsized array parameter. (#4744)
Diffstat (limited to 'docs/user-guide/02-conventional-features.md')
| -rw-r--r-- | docs/user-guide/02-conventional-features.md | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/docs/user-guide/02-conventional-features.md b/docs/user-guide/02-conventional-features.md index fe0fdb854..bd8f0435c 100644 --- a/docs/user-guide/02-conventional-features.md +++ b/docs/user-guide/02-conventional-features.md @@ -111,7 +111,7 @@ In some cases the element count is then inferred from the initial value of a var int a[] = { 1, 2, 3 }; ``` -In other cases, the result is a _runtime-sized_ array, where the actual element count will be determined later: +In other cases, the result is a _unsized_ array, where the actual element count will be determined later: ```hlsl // the type of `b` is `int[]` @@ -119,6 +119,46 @@ void f( int b[] ) { ... } ``` +It is allowed to pass a sized array as argument to an unsized array parameter when calling a function. + +Array types has a `getCount()` memeber function that returns the length of the array. + +```hlsl +int f( int b[] ) +{ + return b.getCount(); // Note: all arguments to `b` must be resolvable to sized arrays. +} + +void test() +{ + int arr[3] = { 1, 2, 3 }; + int x = f(arr); // OK, passing sized array to unsized array parameter, x will be 3. +} +``` + +Please note that if a function calls `getCount()` method on an unsized array parameter, then all +calls to that function must provide a sized array argument, otherwise the compiler will not be able +to resolve the size and will report an error. The following code shows an example of valid and +invalid cases. + +```hlsl +int f( int b[] ) +{ + return b.getCount(); +} +int g( int b[] ) +{ + return f(b); // transitive calls are allowed. +} +uniform int unsizedParam[]; +void test() +{ + g(unsizedParam); // Not OK, `unsizedParam` doesn't have a known size at compile time. + int arr[3]; + g(arr); // OK. +} +``` + There are more limits on how runtime-sized arrays can be used than on arrays of statically-known element count. > #### Note #### |
