diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2017-05-09 17:12:06 +0300 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2017-05-09 18:07:17 +0300 |
commit | 4d38932cb430139975fd1fc13436317b2a16fdc4 (patch) | |
tree | 3d19097565b989ed6fdb571462779c050f4c46e8 /intern | |
parent | e20eb2dec0dd5a9bb1dba6267e1581626d69958d (diff) |
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
Diffstat (limited to 'intern')
-rw-r--r-- | intern/cycles/kernel/kernel_jitter.h | 21 |
1 files changed, 16 insertions, 5 deletions
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; |