[Bf-blender-cvs] [4d38932cb43] master: Cycles: Use more stable version of integer square root function

Sergey Sharybin noreply at git.blender.org
Tue May 9 17:07:23 CEST 2017


Commit: 4d38932cb430139975fd1fc13436317b2a16fdc4
Author: Sergey Sharybin
Date:   Tue May 9 16:12:06 2017 +0200
Branches: master
https://developer.blender.org/rB4d38932cb430139975fd1fc13436317b2a16fdc4

Cycles: Use more stable version of integer square root function

Old code was working quite unreliable in combination with fast math
flag, especially when compiling with Clang. It seems we were hitting
result of the following bug submitted to Clang [1].

Basically, it was happening so that (int)sqrtf(64) was 7 when Cycles
is built with Clang but was correct 8 when built with GCC.

This commit works this around. Annoying, but don't see other way to
keep sampling pattern the same for Clang and GCC.

[1] https://bugs.llvm.org//show_bug.cgi?id=24063

===================================================================

M	intern/cycles/kernel/kernel_jitter.h

===================================================================

diff --git a/intern/cycles/kernel/kernel_jitter.h b/intern/cycles/kernel/kernel_jitter.h
index 67546131746..f5855757d3f 100644
--- a/intern/cycles/kernel/kernel_jitter.h
+++ b/intern/cycles/kernel/kernel_jitter.h
@@ -175,15 +175,26 @@ ccl_device float cmj_sample_1D(int s, int N, int p)
 	return (x + jx)*invN;
 }
 
-ccl_device void cmj_sample_2D(int s, int N, int p, float *fx, float *fy)
+/* TODO(sergey): Do some extra tests and consider moving to util_math.h. */
+ccl_device_inline int cmj_isqrt(int value)
 {
-	kernel_assert(s < N);
-
 #if defined(__KERNEL_CUDA__)
-	int m = float_to_int(__fsqrt_ru(N));
+	return float_to_int(__fsqrt_ru(value));
+#elif defined(__KERNEL_GPU__)
+	return float_to_int(sqrtf(value));
 #else
-	int m = float_to_int(sqrtf(N));
+	/* This is a work around for fast-math on CPU which might replace sqrtf()
+	 * with am approximated version.
+	 */
+	return float_to_int(sqrtf(value) + 1e-6f);
 #endif
+}
+
+ccl_device void cmj_sample_2D(int s, int N, int p, float *fx, float *fy)
+{
+	kernel_assert(s < N);
+
+	int m = cmj_isqrt(N);
 	int n = (N - 1)/m + 1;
 	float invN = 1.0f/N;
 	float invm = 1.0f/m;




More information about the Bf-blender-cvs mailing list