Faster GLSL Raymarching Using Step Relaxation

Sphere tracing step relaxation accelerates convergence in GLSL raymarchers by dynamically scaling the ray's marching distance beyond the conservative bounds provided by a Signed Distance Function (SDF). While standard sphere tracing guarantees safety by only stepping the exact distance to the nearest surface, it often requires excessive iterations when rays travel tangentially across surfaces or through open space. Step relaxation—often referred to as over-relaxation—amplifies each step by a relaxation factor, detects potential overshoots, and corrects them when necessary. This technique substantially decreases total iteration counts, reducing GPU branch overhead and texture or ALU bottlenecks in shader pipelines.

The Limitation of Standard Sphere Tracing

In traditional sphere tracing, the ray advances iteratively along its direction vector \(\vec{v}\) by a step size equal to the SDF value \(d = f(\vec{p})\) evaluated at the current position:

\[\vec{p}_{n+1} = \vec{p}_n + d \cdot \vec{v}\]

Because \(f(\vec{p})\) represents the radius of the largest unoccluded sphere centered at \(\vec{p}\), this step is unconditionally safe and mathematically prevents the ray from tunneling through geometry. However, when a ray travels almost parallel to a surface (grazing angles) or through narrow crevices, the distance to the nearest surface remains small even if the surface along the ray's forward path is far away. The ray marches forward in microscopic increments, stalling convergence and exhausting the loop iteration budget.

How Step Relaxation Works

Step relaxation adapts concepts from numerical analysis (specifically Successive Over-Relaxation) to bypass this conservative bottleneck. Instead of stepping strictly by the evaluated distance \(d\), the algorithm applies a multiplier \(\omega\) (where \(1.0 < \omega < 2.0\), typically around \(1.2\) to \(1.6\)) to extrapolate a larger candidate step:

\[\vec{p}_{\text{candidate}} = \vec{p}_n + \omega \cdot d \cdot \vec{v}\]

By intentionally overstepping, the ray clears open space and traverses flat or low-curvature trajectories in far fewer iterations.

Detecting and Correcting Overshoots

Because stepping by \(\omega \cdot d\) violates the strict spatial safety guarantee of the SDF, the ray risks penetrating geometry, jumping entirely through thin geometry, or causing visual artifacts. To maintain visual fidelity, relaxed sphere tracing employs an overshoot verification step:

  1. Evaluate Candidate: The shader evaluates the distance at the new candidate position, \(d_{\text{candidate}} = f(\vec{p}_{\text{candidate}})\).
  2. Check for Invalidation: A valid step requires that the candidate distance satisfies geometric continuity. If \(d_{\text{candidate}} < 0\), the ray has penetrated a surface. Additionally, based on the Lipschitz condition of SDFs (\(|\nabla f| \le 1\)), the change in distance cannot exceed the distance traveled. If \(d_{\text{candidate}} + \text{step} < d_{\text{previous}}\), the ray has skipped over a feature.
  3. Rollback or Clamp: When an invalid step or overshoot is detected, the algorithm rejects the relaxed candidate. The ray either rolls back to the standard conservative distance step (\(d\)), or adjusts along the segment using linear interpolation between the two sampled distances to position the ray safely near the surface boundary.

Algorithmic Flow in GLSL

In a GLSL fragment shader, this logic is integrated directly into the raymarching loop:

  • Track the previous distance and candidate step size across iterations.
  • Calculate the relaxed step: float stepSize = d * omega;
  • If the previous step overshot (e.g., d + stepSize < prevD), adjust the current position backwards by an error term before calculating the next step.
  • Dynamically reduce \(\omega\) toward \(1.0\) as the ray approaches the surface threshold (e.g., when \(d < \epsilon\)) to ensure sub-pixel convergence without oscillation.

Performance Gains in Raymarchers

Relaxing the step length cuts the total loop iterations required to reach the convergence threshold by roughly 25% to 50%, depending on scene complexity. In GLSL raymarchers, loop iterations correspond directly to SDF evaluations, which frequently contain computationally expensive noise functions, matrix transformations, or trigonometric operations. By skipping redundant evaluations across empty space and grazing regions, step relaxation minimizes instruction count and alleviates register pressure, resulting in lower frametimes and higher rendering throughput.