diff --git a/intern/cycles/render/buffers.cpp b/intern/cycles/render/buffers.cpp index 572cfae45cd..b509134512b 100644 --- a/intern/cycles/render/buffers.cpp +++ b/intern/cycles/render/buffers.cpp @@ -223,7 +223,7 @@ bool RenderBuffers::get_pass_rect(PassType type, float exposure, int sample, int float3 f = make_float3(in[0], in[1], in[2]); float3 f_divide = make_float3(in_divide[0], in_divide[1], in_divide[2]); - f = safe_divide_color(f*exposure, f_divide); + f = safe_divide_even_color(f*exposure, f_divide); pixels[0] = f.x; pixels[1] = f.y; diff --git a/intern/cycles/util/util_math.h b/intern/cycles/util/util_math.h index 14ebf311a4b..cde547cd77c 100644 --- a/intern/cycles/util/util_math.h +++ b/intern/cycles/util/util_math.h @@ -1100,6 +1100,42 @@ __device_inline float3 safe_divide_color(float3 a, float3 b) return make_float3(x, y, z); } +__device_inline float3 safe_divide_even_color(float3 a, float3 b) +{ + float x, y, z; + + x = (b.x != 0.0f)? a.x/b.x: 0.0f; + y = (b.y != 0.0f)? a.y/b.y: 0.0f; + z = (b.z != 0.0f)? a.z/b.z: 0.0f; + + /* try to get grey even if b is zero */ + if(b.x == 0.0f) { + if(b.y == 0.0f) { + x = z; + y = z; + } + else if(b.z == 0.0f) { + x = y; + z = y; + } + else + x = 0.5f*(y + z); + } + else if(b.y == 0.0f) { + if(b.z == 0.0f) { + y = x; + z = x; + } + else + y = 0.5f*(x + z); + } + else if(b.z == 0.0f) { + z = 0.5f*(x + y); + } + + return make_float3(x, y, z); +} + /* Rotation of point around axis and angle */ __device_inline float3 rotate_around_axis(float3 p, float3 axis, float angle)