schell / schell/wgsl-rs

Add vector splat constructors (e.g. vec2f from single scalar)

Open
#88 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Rust
Stars
61
Forks
4
Avg merge
1d 21h
Merged PRs (30d)
9

Description

## Description

WGSL supports scalar splat construction for vectors:

```wgsl
let v = vec2f(3.0); // equivalent to vec2f(3.0, 3.0)
let w = vec3f(1.0); // equivalent to vec3f(1.0, 1.0, 1.0)
```

The wgsl-rs constructors (`vec2f`, `vec3f`, `vec4f`) require all components explicitly. This adds verbosity when translating shaders from GLSL (which also supports `vec2(scalar)`) or writing new shaders that use uniform scaling, splatted thresholds, etc.

```rust
// Current
let u = f * f * (vec2f(3.0, 3.0) - 2.0 * f);

// With splat constructor
let u = f * f * (vec2f_splat(3.0) - 2.0 * f);
```

## Suggested approach

Add `vec2f_splat(x: f32) -> Vec2f`, `vec3f_splat(x: f32) -> Vec3f`, `vec4f_splat(x: f32) -> Vec4f` (and equivalents for integer types). These would map to the single-argument WGSL vector constructors `vec2f(x)`, `vec3f(x)`, `vec4f(x)` via `BUILTIN_CASE_NAME_MAP` or similar.

## Location

`crates/wgsl-rs/src/std/vector.rs`, alongside the existing constructor functions.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.