diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-21 08:42:13 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-21 08:42:13 +0100 |
| commit | 25dc87528915fb0fd89181333324b215d77ad014 (patch) | |
| tree | 5994db01a9d68a8d4166fa9ee5058c2b793b9795 | |
| parent | fc40d39ac302fae6f5fe726c6bc077f53580c052 (diff) | |
refactor(wgsl): Factorize getScreenCoord helper
Factorizes the screen coordinate calculation from scene1.wgsl into a reusable getScreenCoord function in common/shaders/math/common_utils.wgsl.
This improves code reuse and simplifies fragment shaders.
| -rw-r--r-- | common/shaders/math/common_utils.wgsl | 10 | ||||
| -rw-r--r-- | doc/COMPLETED.md | 10 | ||||
| -rw-r--r-- | workspaces/main/shaders/scene1.wgsl | 18 |
3 files changed, 26 insertions, 12 deletions
diff --git a/common/shaders/math/common_utils.wgsl b/common/shaders/math/common_utils.wgsl index 7131216..49aaead 100644 --- a/common/shaders/math/common_utils.wgsl +++ b/common/shaders/math/common_utils.wgsl @@ -34,3 +34,13 @@ fn grid_pattern(uv: vec2<f32>) -> f32 { // NOTE: calc_sdf_normal_bumped() removed - too specialized, depends on get_dist() // from scene_query snippets. Keep bump mapping code inline in shaders that use it. + +// Calculates normalized screen coordinates from fragment position and resolution. +// Input `p` is the fragment's @builtin(position), `resolution` is the screen resolution. +// Returns a vec2<f32> in NDC space, with X adjusted for aspect ratio. +fn getScreenCoord(p: vec4<f32>, resolution: vec2<f32>) -> vec2<f32> { + let q = p.xy / resolution; + var coord = -1.0 + 2.0 * q; + coord.x *= resolution.x / resolution.y; + return coord; +} diff --git a/doc/COMPLETED.md b/doc/COMPLETED.md index 72e389d..0dba307 100644 --- a/doc/COMPLETED.md +++ b/doc/COMPLETED.md @@ -29,6 +29,16 @@ Detailed historical documents have been moved to `doc/archive/` for reference: Use `read @doc/archive/FILENAME.md` to access archived documents. +## Recently Completed (February 21, 2026) + +- [x] **WGSL Refactor: getScreenCoord Helper** + - **Goal**: Factorize boilerplate screen coordinate calculation into a reusable helper function. + - **Implementation**: + - Created `getScreenCoord(p, resolution)` function in `common/shaders/math/common_utils.wgsl`. It takes a fragment position and resolution, and returns aspect-corrected normalized device coordinates. + - Updated `workspaces/main/shaders/scene1.wgsl` to `#include "math/common_utils.wgsl"` and replaced the manual coordinate calculation with a call to the new helper. + - **Impact**: Reduces code duplication and simplifies fragment shaders. + - **Files**: `common/shaders/math/common_utils.wgsl`, `workspaces/main/shaders/scene1.wgsl`. + ## Recently Completed (February 20, 2026) - [x] **Port Scene1Effect + Fix seq_compiler Timing Bug** diff --git a/workspaces/main/shaders/scene1.wgsl b/workspaces/main/shaders/scene1.wgsl index a47413b..f6a6c38 100644 --- a/workspaces/main/shaders/scene1.wgsl +++ b/workspaces/main/shaders/scene1.wgsl @@ -6,6 +6,7 @@ #include "math/color" #include "math/utils" #include "math/sdf_shapes" +#include "math/common_utils" #include "render/raymarching" @group(0) @binding(2) var<uniform> uniforms: UniformsSequenceParams; @@ -136,7 +137,7 @@ fn render1(ray: Ray) -> vec3<f32> { let result = rayMarchWithID(ray.origin, ray.direction, init); if (result.distance < MAX_RAY_LENGTH) { - let nsp = reconstructPosition(ray.origin, ray.direction, result); + let nsp = reconstructPosition(ray, result); let nnor = normalWithID(nsp); let nref = reflect(ray.direction, nnor); @@ -149,7 +150,7 @@ fn render1(ray: Ray) -> vec3<f32> { var nrcol = render0(rRay); if (nrt_result.distance < MAX_RAY_LENGTH) { - let nrsp = reconstructPosition(nsp, nref, nrt_result); + let nrsp = reconstructPosition(Ray(nsp, nref), nrt_result); let nrnor = normalWithID(nrsp); let nrref = reflect(nref, nrnor); nrcol = boxCol(nrcol, nrsp, nref, nrnor, render0(Ray(nrsp, nrref)), 1.0, 1.0); @@ -166,19 +167,12 @@ fn render1(ray: Ray) -> vec3<f32> { return col; } -fn effect(p: vec2<f32>) -> vec3<f32> { -// g_rot0 = rot(-0.2 * uniforms.time); - let ray = getCameraRay(camera, p); - return render1(ray); -} - #include "render/fullscreen_vs" @fragment fn fs_main(@builtin(position) p: vec4<f32>) -> @location(0) vec4<f32> { - let q = p.xy / uniforms.resolution; - var coord = -1.0 + 2.0 * q; - coord.x *= uniforms.resolution.x / uniforms.resolution.y; - var col = effect(coord); + let coord = getScreenCoord(p, uniforms.resolution); + let ray = getCameraRay(camera, coord); + var col = render1(ray); col = aces_approx(col); col = sRGB(col); return vec4<f32>(col, 1.0); |
