Skip to content

cvcGL eWave: the update pass's own sin and cos, so macOS's renderer agrees with the CPU - #1224

Merged
transfix merged 3 commits into
masterfrom
fix/ewave-sincos
Nov 14, 2026
Merged

transfix merged 3 commits into
masterfrom
fix/ewave-sincos

Conversation

@transfix

@transfix transfix commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

cvcgl_ewave fails on every macOS package lane, so master's push runs show the red too (#1201's package-macos / libcvc-release-grpc, job 132021840192, is one). On macOS CI's Apple Software Renderer the GPU eWave window drifts from the CPU one by about 8e-4 of the field per step, reaching 3.0e-2 after the test's 48 steps; the test allows 1e-3. The drop as added matches to 2e-8, so the float textures are fine; the error builds up in the step.

Cause

The update pass took cos(om dt) and sin(om dt) from GLSL's built-ins. GLSL bounds those only to an absolute 2^-11 (4.9e-4) on [-pi, pi], and that renderer appears to use the slack. llvmpipe and real GPUs are far tighter, so Linux and Windows pass. The FFT's twiddles come from a table computed on the CPU, which is why cvcgl_ocean_fft passes on macOS.

Fix

The pass computes sin and cos itself (sinCos in EWave.cpp):

  • x less the nearest multiple of pi / 2, with pi / 2 in two parts;
  • the Taylor series to r^9 and r^10 on |r| <= pi / 4;
  • the quadrant.

In float32, with one rounding per operation and no FMA, that is within 8.4e-8 of sin and cos on [0, 256] rad (checked with numpy). The coefficients stay per texel from uniforms, so a new dt still costs nothing. sqrt, tanh and exp keep GLSL's built-ins, which GLSL bounds in ulps.

Checked locally (Windows, Intel Iris Xe, cvcgl_ewave Release)

master master, sin/cos off by up to 4.5e-4 this PR
h drift, step 48 1.95e-5 1.19e-2 8.71e-6
phi drift, step 48 1.30e-5 1.22e-2 9.15e-6
wall run vs CPU 5.62e-5 1.41e-2 1.04e-5
result PASS FAIL (the same 3 checks as macOS) PASS
GPU per step 0.84-0.91 ms 0.71-0.89 ms

The middle column was a throwaway experiment, not part of this PR. It gave the old shader built-ins that are wrong by an amount GLSL allows, and it reproduced macOS's three failures, with the same steady growth and the same order of magnitude.

Not checked here: macOS itself. PRs no longer run the macOS package lanes (#1205), so master's push run after this merges is the first macOS check. llvmpipe is covered by this PR's Linux and Windows jobs.

After #1194 (the symmetric step with no-flux walls)

#1194's multiply pass computes kappa~ = 4 sin^2(omega dt_s / 2) / (g dt_s^2) with GLSL's sin. At the default 1/60 s substep that argument is about 0.1 rad or less, so GLSL's absolute 2^-11 is a relative error of several percent: 4.9% at 0.01 rad. sinCos is now one string literal, CVC_EWAVE_SINCOS_GLSL, and both passes take it in. The multiply pass uses its .x, which is within 1 float ulp (relative) on [1e-6, 0.5] rad. No eWave shader calls the built-in sin or cos now.

Checked locally on the merged tree (Intel Iris Xe), 3 runs:

  • this PR: cvcgl_ewave passes. Drift after 48 steps is h 3.1e-6 and phi 2.2e-6, and the no-flux and zero walls match the CPU to 5.1e-6 and 1.0e-5. The energy check holds on both 10,000-step jittered runs.
  • throwaway experiment: only the multiply pass got a sin off by up to 4.5e-4, within GLSL's bound. It fails 6 checks. Drift reaches 4.5e-2, the no-flux wall run is off from the CPU by 6.8e-2, and the walled solver's energy rises above its start.

…grees with the CPU

cvcgl_ewave failed on every macOS package lane: on the Apple Software Renderer the GPU window left
the CPU one by about 8e-4 of the field a step, 3e-2 after the test's 48 steps, against its 1e-3.
The update pass took cos(om dt) and sin(om dt) from GLSL's built-ins, which GLSL bounds only to an
absolute 2^-11 (4.9e-4) on [-pi, pi]; that renderer uses the slack, llvmpipe and real GPUs don't.

The pass now computes them itself: x less the nearest multiple of pi / 2 (two-part pi / 2), the
series to r^9 and r^10 on |r| <= pi / 4, then the quadrant. In float with one rounding per
operation that is within 8.4e-8 of sin and cos on [0, 256] rad. The coefficients stay per texel
from uniforms, so a new dt still costs nothing; sqrt, tanh and exp stay GLSL's (bounded in ulps).
@transfix
transfix enabled auto-merge (squash) November 14, 2026 06:49
#1194's symmetric step (no-flux walls, the default) takes kappa~ = 4 sin^2(omega dt_s / 2) /
(g dt_s^2) in its multiply pass, from GLSL's sin: at the default substep the argument is a tenth of
a radian or less, where GLSL's absolute 2^-11 is a relative error of several percent. sinCos is now
one string literal (CVC_EWAVE_SINCOS_GLSL) that the update and multiply passes both take in, and
the multiply pass's sin is its .x. EWave.h keeps #1194's description, with where sin and cos come
from.
@transfix
transfix merged commit cf7aa5c into master Nov 14, 2026
28 checks passed
@transfix
transfix deleted the fix/ewave-sincos branch November 14, 2026 17:20
transfix added a commit that referenced this pull request Nov 14, 2026
…urfaces

A clean merge. #1225 makes master build again after #1203; its cmd_trace
change is the one this branch already had. The diff against master is only
this PR's own 14 files again.
transfix added a commit that referenced this pull request Nov 14, 2026
…ates

#1194 gave EWave the CPU's symmetric step for no-flux walls (fill, multiply
and gather passes) and #1224 gave the update and multiply passes their own sin
and cos series. Both rewrote the code this branch changed, so EWave.cpp and
EWave.h start from master's and take this branch's change on both paths:

- The exact step reads each mode's coefficients (modeCoefficients, as
  ewave::prepare) and each cell's real-space factor (realFactors, as
  ewave::update_real) from textures worked out on the host in double.
- The symmetric step's multiply pass reads kappa~ and damping_k2's 1 - F
  (symmetricMultipliers, as ewave::prepare_symmetric, -expm1 for 1 - F), and
  its gather pass reads the real-space factors for dt_s.
- The last pass home only zeroes walls, as before. No pass takes a sin, cos,
  exp, sqrt or tanh of its own, so #1224's series goes.
- Each table is worked out again only when the step, the depth (setDepth) or
  the mask changes; dt_s is fixed, so the symmetric step pays once.

cvcgl_ewave and cvcgl_wake pass on Windows (Intel Arc). Against master's
#1224 on the same machine: step 48 (no-flux walls) h 3.14e-6 -> 2.20e-6, phi
2.23e-6 -> 1.53e-6; the zero-wall run 1.04e-5 -> 9.62e-6, the no-flux wall run
5.05e-6 -> 3.63e-6. Step times within run-to-run noise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant