Chris Healy
9ed5dcdc27
GPU: Use inversesqrt for light distance and half-vector calculations
Replace length()/normalize() with inversesqrt() pattern for better
performance on mobile GPUs. The inversesqrt instruction is a single
hardware operation, while length() requires sqrt(dot()) and normalize()
requires inversesqrt internally plus a multiply.
Changes:
1. Light distance calculation:
Before: distance = length(toLight); toLight /= distance;
After: distSq = dot(toLight, toLight);
invDist = inversesqrt(distSq);
distance = distSq * invDist;
toLight *= invDist;
2. Specular half-vector normalization:
Before: ldot = dot(normalize(toLight + vec3(0,0,1)), worldnormal);
After: halfVec = toLight + vec3(0,0,1);
halfInvLen = inversesqrt(dot(halfVec, halfVec));
ldot = dot(halfVec, worldnormal) * halfInvLen;
Also reuses distSq in attenuation calculation to avoid recomputing
distance*distance.
Offline shader compiler results (vertex shader with 4-light ubershader):
Mali (malioc):
G31 (Bifrost): 44.90 -> 40.50 cycles (-9.8%)
G52 (Bifrost): 14.97 -> 13.50 cycles (-9.8%)
G310 (Valhall): 13.88 -> 12.88 cycles (-7.2%)
G57 (Valhall): 7.60 -> 6.97 cycles (-8.3%)
PowerVR (Profiling Compilers):
Series 8 GE8300: 322 -> 302 ALU cycles (-6.2%)
Series 9 GM9445: 296 -> 280 ALU cycles (-5.4%)
Series B BXM: 560 -> 540 instructions (-3.6%)
Signed-off-by: Chris Healy <cphealy@gmail.com>
2026-03-08 19:13:41 -07:00
..
2025-08-29 23:12:06 +02:00
2025-01-05 09:18:13 +01:00
2025-05-14 15:13:59 +02:00
2025-05-14 15:13:59 +02:00
2024-12-18 13:57:26 +01:00
2025-01-17 18:06:24 +01:00
2026-02-05 13:26:17 +01:00
2026-01-30 14:10:36 +01:00
2025-06-10 15:07:16 +02:00
2025-06-10 16:59:14 +02:00
2026-02-24 00:56:45 +01:00
2026-02-19 11:24:46 +01:00
2023-12-29 08:27:56 +01:00
2025-12-30 20:31:07 +01:00
2026-02-13 14:56:31 +01:00
2026-01-23 23:13:04 +01:00
2025-11-05 12:49:51 +01:00
2024-12-19 16:23:20 +01:00
2024-12-21 14:28:00 +01:00
2025-11-05 12:49:52 +01:00
2026-02-02 09:44:18 +01:00
2026-02-08 10:46:16 +01:00
2026-02-08 10:13:34 +01:00
2024-12-18 13:57:26 +01:00
2024-07-25 14:52:41 +02:00
2026-02-05 11:12:53 +01:00
2024-12-01 14:17:01 +01:00
2023-07-30 14:42:22 +02:00
2025-05-14 15:13:59 +02:00
2025-05-14 15:13:59 +02:00
2025-08-29 23:12:06 +02:00
2023-05-25 10:19:01 +02:00
2026-02-05 13:26:17 +01:00
2025-11-23 20:37:37 +01:00
2024-12-18 13:57:26 +01:00
2025-06-10 15:03:15 +02:00
2025-04-10 21:33:05 +02:00
2026-02-12 11:03:46 +01:00
2026-01-29 23:16:51 +01:00
2025-07-19 01:30:50 +08:00
2024-12-18 13:57:26 +01:00
2026-02-11 01:24:11 +01:00
2025-07-08 13:51:09 +02:00
2024-12-19 16:23:20 +01:00
2024-04-05 17:04:31 +03:00
2024-12-19 09:53:07 +01:00
2024-10-29 08:18:41 +01:00
2024-12-18 13:57:26 +01:00
2024-12-18 13:57:26 +01:00
2025-09-24 10:52:09 -06:00
2025-09-24 10:52:09 -06:00
2026-02-17 11:59:30 +01:00
2026-02-05 13:26:17 +01:00
2026-02-10 13:59:32 +01:00
2024-05-11 10:00:39 +02:00
2025-09-24 10:52:09 -06:00
2025-09-24 10:52:09 -06:00
2025-09-24 10:52:09 -06:00
2026-03-08 19:13:41 -07:00
2025-06-10 16:59:14 +02:00