diff options
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/CODING_STYLE.md | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/doc/CODING_STYLE.md b/doc/CODING_STYLE.md index 533cffb..5c86409 100644 --- a/doc/CODING_STYLE.md +++ b/doc/CODING_STYLE.md @@ -107,3 +107,30 @@ One-line comment for non-obvious functions. ``` Three-line header for all source files. + +--- + +## WGSL Shader Style + +### Return vs Pointer Parameters + +**Rule:** Prefer return values over pointer parameters for small structs (≤16 bytes). + +```wgsl +// Correct - return value (12 bytes: 3×f32) +fn rayMarchWithID(ro: vec3<f32>, rd: vec3<f32>, init: RayMarchResult) -> RayMarchResult { + var result = init; + // ... modify result + return result; +} + +// Wrong - pointer parameter (unnecessary complexity) +fn rayMarchWithID(ro: vec3<f32>, rd: vec3<f32>, result: ptr<function, RayMarchResult>) { + // ... modify *result +} +``` + +**Rationale:** +- Small structs (≤16 bytes) are efficiently handled by GPU return value optimization +- Functional style is clearer and less error-prone +- `ptr<function, T>` adds complexity with no performance gain for small types |
