From 08c1f5bf335992ff797e9543f50b1b90f96e8369 Mon Sep 17 00:00:00 2001 From: Antony Ryakiotakis Date: Sat, 23 May 2020 14:21:49 +0200 Subject: [PATCH] Cycles: Improve sampling of area lights This patch uses the sampling method described in "A Low Distortion Map Between Triangle and Square" by Eric Heitz. The benefit is avoiding sqrt in the calculation, which could be cheaper on some architectures, and the result is more even sampling across the triangle surface. Based on ideas from https://pharr.org/matt/blog/2019/02/27/triangle-sampling-1.html https://pharr.org/matt/blog/2019/03/13/triangle-sampling-1.5.html Reviewed By: Brecht Van Lommel Differential Revision: https://developer.blender.org/D6566 --- intern/cycles/kernel/kernel_light.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/intern/cycles/kernel/kernel_light.h b/intern/cycles/kernel/kernel_light.h index ce908ce0fe2..d918abed381 100644 --- a/intern/cycles/kernel/kernel_light.h +++ b/intern/cycles/kernel/kernel_light.h @@ -1041,11 +1041,19 @@ ccl_device_forceinline void triangle_light_sample(KernelGlobals *kg, } } else { - /* compute random point in triangle */ - randu = sqrtf(randu); + /* compute random point in triangle. From Eric Heitz's "A Low-Distortion Map Between Triangle + * and Square" */ + float u = randu; + float v = randv; + if (v > u) { + u *= 0.5f; + v -= u; + } + else { + v *= 0.5f; + u -= v; + } - const float u = 1.0f - randu; - const float v = randv * randu; const float t = 1.0f - u - v; ls->P = u * V[0] + v * V[1] + t * V[2]; /* compute incoming direction, distance and pdf */