diff options
author | William Leeson <leesonw> | 2021-12-10 11:02:55 +0300 |
---|---|---|
committer | William Leeson <william@blender.org> | 2021-12-10 11:09:20 +0300 |
commit | 57f46b9d5f370dfacc92786dab596060400a21a8 (patch) | |
tree | 0f0f1e8300ee3311629ab55c88ed094c8b613d51 /intern | |
parent | 566a458950ee30f60400a7624746342d19cf9695 (diff) |
Fix T92036: `Magic Texture` in Volumetric World Shaders render differently with the CPU and GPU
When rendering volume surfaces in unbounded worlds the volume stepping can produce large values. If used with a magic texture node the values can results in a Inf float which when used in a sin or cos produces a NaN.
To fix this the input values are mapped into the periodic range of the sin and cos functions (-2*PI 2*PI) this stops the possibility of a Inf occurring and thus the NaN. It also improves the accuracy and smoothness of the result due to loss of precision when large values are summed with smaller ones effectively removing the parts of the smaller number (i.e. those in the -2PI to 2PI range) that result in variation of the output of sin and cos.
Reviewed By: brecht
Maniphest Tasks: T92036
Differential Revision: https://developer.blender.org/D12821
Diffstat (limited to 'intern')
-rw-r--r-- | intern/cycles/kernel/osl/shaders/node_magic_texture.osl | 15 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/magic.h | 26 |
2 files changed, 30 insertions, 11 deletions
diff --git a/intern/cycles/kernel/osl/shaders/node_magic_texture.osl b/intern/cycles/kernel/osl/shaders/node_magic_texture.osl index 476c6895f05..0ed83aae3b8 100644 --- a/intern/cycles/kernel/osl/shaders/node_magic_texture.osl +++ b/intern/cycles/kernel/osl/shaders/node_magic_texture.osl @@ -17,14 +17,17 @@ #include "stdcycles.h" /* Magic */ - -color magic(point p, int n, float distortion) +color magic(point p, float scale, int n, float distortion) { float dist = distortion; - float x = sin((p[0] + p[1] + p[2]) * 5.0); - float y = cos((-p[0] + p[1] - p[2]) * 5.0); - float z = -cos((-p[0] - p[1] + p[2]) * 5.0); + float a = mod(p.x * scale, M_2PI); + float b = mod(p.y * scale, M_2PI); + float c = mod(p.z * scale, M_2PI); + + float x = sin((a + b + c) * 5.0); + float y = cos((-a + b - c) * 5.0); + float z = -cos((-a - b + c) * 5.0); if (n > 0) { x *= dist; @@ -103,6 +106,6 @@ shader node_magic_texture(int use_mapping = 0, if (use_mapping) p = transform(mapping, p); - Color = magic(p * Scale, depth, Distortion); + Color = magic(p, Scale, depth, Distortion); Fac = (Color[0] + Color[1] + Color[2]) * (1.0 / 3.0); } diff --git a/intern/cycles/kernel/svm/magic.h b/intern/cycles/kernel/svm/magic.h index f103a8eebcc..715a47d88cf 100644 --- a/intern/cycles/kernel/svm/magic.h +++ b/intern/cycles/kernel/svm/magic.h @@ -20,11 +20,27 @@ CCL_NAMESPACE_BEGIN /* Magic */ -ccl_device_noinline_cpu float3 svm_magic(float3 p, int n, float distortion) +ccl_device_noinline_cpu float3 svm_magic(float3 p, float scale, int n, float distortion) { - float x = sinf((p.x + p.y + p.z) * 5.0f); - float y = cosf((-p.x + p.y - p.z) * 5.0f); - float z = -cosf((-p.x - p.y + p.z) * 5.0f); + /* + * Prevent NaNs due to input p + * Sin and Cosine are periodic about [0 2*PI) so the following + * will yeild a more accurate result. As it stops the input values + * going out of range for floats which caused a NaN. The + * calculation of (px + py + pz)*5 can cause an Inf when one or more + * values are very large the cos or sin of this results in a NaN + * It also addresses the case where one dimension is large relative + * to another which caused banding due to the loss of precision in the + * smaller value. This is due to the value in the -2*PI to 2*PI range + * effectively being lost due to floating point precision. + */ + float px = fmodf(p.x, M_2PI_F); + float py = fmodf(p.y, M_2PI_F); + float pz = fmodf(p.z, M_2PI_F); + + float x = sinf((px + py + pz) * 5.0f * scale); + float y = cosf((-px + py - pz) * 5.0f * scale); + float z = -cosf((-px - py + pz) * 5.0f * scale); if (n > 0) { x *= distortion; @@ -103,7 +119,7 @@ ccl_device_noinline int svm_node_tex_magic( float scale = stack_load_float_default(stack, scale_offset, node2.x); float distortion = stack_load_float_default(stack, distortion_offset, node2.y); - float3 color = svm_magic(co * scale, depth, distortion); + float3 color = svm_magic(co, scale, depth, distortion); if (stack_valid(fac_offset)) stack_store_float(stack, fac_offset, average(color)); |