diff options
author | Mikhail <ktdfly> | 2021-03-15 19:59:38 +0300 |
---|---|---|
committer | Brecht Van Lommel <brecht@blender.org> | 2021-03-15 20:01:57 +0300 |
commit | 21bc1a99baa765d81c3203fd2e451681b8a7fd55 (patch) | |
tree | b0667ffe39bcc13e85129ab2257ada8f853bee93 /intern/cycles/kernel/shaders | |
parent | fbe0165aad9685e6f264956e1232cfe107679d8b (diff) |
Cycles: optimize ensure_valid_reflection(), reduces render time by about 1%
This is an implementation that is about 1.5-2.1 times faster. It gives a result
that is on average 6° different from the old implementation. The difference is
because normals (Ng, N, N') are not selected to be coplanar, but instead
reflection R is lifted the least amount and the N' is computed as a bisector.
Differential Revision: https://developer.blender.org/D10084
Diffstat (limited to 'intern/cycles/kernel/shaders')
-rw-r--r-- | intern/cycles/kernel/shaders/stdcycles.h | 69 |
1 files changed, 16 insertions, 53 deletions
diff --git a/intern/cycles/kernel/shaders/stdcycles.h b/intern/cycles/kernel/shaders/stdcycles.h index dd604da68ce..af7b645d9a2 100644 --- a/intern/cycles/kernel/shaders/stdcycles.h +++ b/intern/cycles/kernel/shaders/stdcycles.h @@ -84,67 +84,30 @@ closure color principled_hair(normal N, closure color henyey_greenstein(float g) BUILTIN; closure color absorption() BUILTIN; -normal ensure_valid_reflection(normal Ng, vector I, normal N) +normal ensure_valid_reflection(normal Ng, normal I, normal N) { /* The implementation here mirrors the one in kernel_montecarlo.h, * check there for an explanation of the algorithm. */ - - float sqr(float x) - { - return x * x; - } - - vector R = 2 * dot(N, I) * N - I; - - float threshold = min(0.9 * dot(Ng, I), 0.01); - if (dot(Ng, R) >= threshold) { - return N; - } - - float NdotNg = dot(N, Ng); - vector X = normalize(N - NdotNg * Ng); - - float Ix = dot(I, X), Iz = dot(I, Ng); - float Ix2 = sqr(Ix), Iz2 = sqr(Iz); - float a = Ix2 + Iz2; - - float b = sqrt(Ix2 * (a - sqr(threshold))); - float c = Iz * threshold + a; - - float fac = 0.5 / a; - float N1_z2 = fac * (b + c), N2_z2 = fac * (-b + c); - int valid1 = (N1_z2 > 1e-5) && (N1_z2 <= (1.0 + 1e-5)); - int valid2 = (N2_z2 > 1e-5) && (N2_z2 <= (1.0 + 1e-5)); - - float N_new_x, N_new_z; - if (valid1 && valid2) { - float N1_x = sqrt(1.0 - N1_z2), N1_z = sqrt(N1_z2); - float N2_x = sqrt(1.0 - N2_z2), N2_z = sqrt(N2_z2); - - float R1 = 2 * (N1_x * Ix + N1_z * Iz) * N1_z - Iz; - float R2 = 2 * (N2_x * Ix + N2_z * Iz) * N2_z - Iz; - - valid1 = (R1 >= 1e-5); - valid2 = (R2 >= 1e-5); - if (valid1 && valid2) { - N_new_x = (R1 < R2) ? N1_x : N2_x; - N_new_z = (R1 < R2) ? N1_z : N2_z; - } - else { - N_new_x = (R1 > R2) ? N1_x : N2_x; - N_new_z = (R1 > R2) ? N1_z : N2_z; + vector R; + float NI = dot(N, I); + float NgR, threshold; + + if (NI > 0) { + R = (2 * NI) * N - I; + NgR = dot(Ng, R); + threshold = min(0.9 * dot(Ng, I), 0.01); + if (NgR >= threshold) { + return N; } } - else if (valid1 || valid2) { - float Nz2 = valid1 ? N1_z2 : N2_z2; - N_new_x = sqrt(1.0 - Nz2); - N_new_z = sqrt(Nz2); - } else { - return Ng; + R = -I; + NgR = dot(Ng, R); + threshold = 0.01; } - return N_new_x * X + N_new_z * Ng; + R = R + Ng * (threshold - NgR); + return normalize(I * length(R) + R * length(I)); } #endif /* CCL_STDOSL_H */ |