diff options
author | Sv. Lockal <lockalsash@gmail.com> | 2015-05-05 09:11:54 +0300 |
---|---|---|
committer | Sv. Lockal <lockalsash@gmail.com> | 2015-05-05 09:11:54 +0300 |
commit | 7201f6d14c0161ad9a0d4143d1c5caf872e0d93e (patch) | |
tree | cf3d0b69ed4a1ad54a42c4c9322df396ab3e2a0e /intern/cycles/kernel | |
parent | 22bbd1c51219aafb40adb3e9a206f660a621fd70 (diff) |
Cycles: Use curve approximation for blackbody instead of lookup table
Now we calculate color in range 800..12000 using an approximation a/x+bx+c for R and G and ((at + b)t + c)t + d) for B.
Max absolute error for RGB for non-lut function is less than 0.0001, which is enough to get the same 8 bit/channel color as for OSL with a noticeable performance difference.
However there is a slight visible difference between previous non-OSL implementation because of lookup table interpolation and offset-by-one mistake.
The previous implementation gave black color outside of soft range (t > 12000), now it gives the same color as for 12000.
Also blackbody node without input connected is being converted to value input at shader compile time.
Reviewers: dingto, sergey
Reviewed By: dingto
Subscribers: nutel, brecht, juicyfruit
Differential Revision: https://developer.blender.org/D1280
Diffstat (limited to 'intern/cycles/kernel')
-rw-r--r-- | intern/cycles/kernel/kernel_types.h | 9 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm.h | 3 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_blackbody.h | 38 | ||||
-rw-r--r-- | intern/cycles/kernel/svm/svm_math_util.h | 62 |
4 files changed, 66 insertions, 46 deletions
diff --git a/intern/cycles/kernel/kernel_types.h b/intern/cycles/kernel/kernel_types.h index b948f7de2f4..0491b8ddec7 100644 --- a/intern/cycles/kernel/kernel_types.h +++ b/intern/cycles/kernel/kernel_types.h @@ -38,12 +38,6 @@ CCL_NAMESPACE_BEGIN #define BSSRDF_MIN_RADIUS 1e-8f #define BSSRDF_MAX_HITS 4 -#define BB_DRAPER 800.0f -#define BB_MAX_TABLE_RANGE 12000.0f -#define BB_TABLE_XPOWER 1.5f -#define BB_TABLE_YPOWER 5.0f -#define BB_TABLE_SPACING 2.0f - #define BECKMANN_TABLE_SIZE 256 #define TEX_NUM_FLOAT_IMAGES 5 @@ -989,9 +983,8 @@ typedef struct KernelCurves { } KernelCurves; typedef struct KernelTables { - int blackbody_offset; int beckmann_offset; - int pad1, pad2; + int pad1, pad2, pad3; } KernelTables; typedef struct KernelData { diff --git a/intern/cycles/kernel/svm/svm.h b/intern/cycles/kernel/svm/svm.h index dd9173d92b3..1598019856a 100644 --- a/intern/cycles/kernel/svm/svm.h +++ b/intern/cycles/kernel/svm/svm.h @@ -142,6 +142,8 @@ CCL_NAMESPACE_END #include "svm_noise.h" #include "svm_texture.h" +#include "svm_math_util.h" + #include "svm_attribute.h" #include "svm_gradient.h" #include "svm_blackbody.h" @@ -164,7 +166,6 @@ CCL_NAMESPACE_END #include "svm_mapping.h" #include "svm_normal.h" #include "svm_wave.h" -#include "svm_math_util.h" #include "svm_math.h" #include "svm_mix.h" #include "svm_ramp.h" diff --git a/intern/cycles/kernel/svm/svm_blackbody.h b/intern/cycles/kernel/svm/svm_blackbody.h index b2ff97d71e6..b750ad87b7f 100644 --- a/intern/cycles/kernel/svm/svm_blackbody.h +++ b/intern/cycles/kernel/svm/svm_blackbody.h @@ -36,46 +36,10 @@ CCL_NAMESPACE_BEGIN ccl_device void svm_node_blackbody(KernelGlobals *kg, ShaderData *sd, float *stack, uint temperature_offset, uint col_offset) { - /* Output */ - float3 color_rgb = make_float3(0.0f, 0.0f, 0.0f); - /* Input */ float temperature = stack_load_float(stack, temperature_offset); - if(temperature < BB_DRAPER) { - /* just return very very dim red */ - color_rgb = make_float3(1.0e-6f,0.0f,0.0f); - } - else if(temperature <= BB_MAX_TABLE_RANGE) { - /* This is the overall size of the table */ - const int lookuptablesize = 956; - const float lookuptablenormalize = 1.0f/956.0f; - - /* reconstruct a proper index for the table lookup, compared to OSL we don't look up two colors - just one (the OSL-lerp is also automatically done for us by "lookup_table_read") */ - float t = powf((temperature - BB_DRAPER) * (1.0f / BB_TABLE_SPACING), (1.0f / BB_TABLE_XPOWER)); - - int blackbody_table_offset = kernel_data.tables.blackbody_offset; - - /* Retrieve colors from the lookup table */ - float lutval = t*lookuptablenormalize; - float R = lookup_table_read(kg, lutval, blackbody_table_offset, lookuptablesize); - lutval = (t + 319.0f*1.0f)*lookuptablenormalize; - float G = lookup_table_read(kg, lutval, blackbody_table_offset, lookuptablesize); - lutval = (t + 319.0f*2.0f)*lookuptablenormalize; - float B = lookup_table_read(kg, lutval, blackbody_table_offset, lookuptablesize); - - R = powf(R, BB_TABLE_YPOWER); - G = powf(G, BB_TABLE_YPOWER); - B = powf(B, BB_TABLE_YPOWER); - - color_rgb = make_float3(R, G, B); - } - - /* Luminance */ - float l = linear_rgb_to_gray(color_rgb); - if(l != 0.0f) - color_rgb /= l; + float3 color_rgb = svm_math_blackbody_color(temperature); if(stack_valid(col_offset)) stack_store_float3(stack, col_offset, color_rgb); diff --git a/intern/cycles/kernel/svm/svm_math_util.h b/intern/cycles/kernel/svm/svm_math_util.h index ff9e662e931..645cbd3fc73 100644 --- a/intern/cycles/kernel/svm/svm_math_util.h +++ b/intern/cycles/kernel/svm/svm_math_util.h @@ -104,5 +104,67 @@ ccl_device float svm_math(NodeMath type, float Fac1, float Fac2) return Fac; } +ccl_device float3 svm_math_blackbody_color(float t) { + /* Calculate color in range 800..12000 using an approximation + * a/x+bx+c for R and G and ((at + b)t + c)t + d) for B + * Max absolute error for RGB is (0.00095, 0.00077, 0.00057), + * which is enough to get the same 8 bit/channel color. + */ + + const float rc[6][3] = { + { 2.52432244e+03f, -1.06185848e-03f, 3.11067539e+00f }, + { 3.37763626e+03f, -4.34581697e-04f, 1.64843306e+00f }, + { 4.10671449e+03f, -8.61949938e-05f, 6.41423749e-01f }, + { 4.66849800e+03f, 2.85655028e-05f, 1.29075375e-01f }, + { 4.60124770e+03f, 2.89727618e-05f, 1.48001316e-01f }, + { 3.78765709e+03f, 9.36026367e-06f, 3.98995841e-01f }, + }; + + const float gc[6][3] = { + { -7.50343014e+02f, 3.15679613e-04f, 4.73464526e-01f }, + { -1.00402363e+03f, 1.29189794e-04f, 9.08181524e-01f }, + { -1.22075471e+03f, 2.56245413e-05f, 1.20753416e+00f }, + { -1.42546105e+03f, -4.01730887e-05f, 1.44002695e+00f }, + { -1.18134453e+03f, -2.18913373e-05f, 1.30656109e+00f }, + { -5.00279505e+02f, -4.59745390e-06f, 1.09090465e+00f }, + }; + + const float bc[6][4] = { + { 0.0f, 0.0f, 0.0f, 0.0f }, /* zeros should be optimized by compiler */ + { 0.0f, 0.0f, 0.0f, 0.0f }, + { 0.0f, 0.0f, 0.0f, 0.0f }, + { -2.02524603e-11f, 1.79435860e-07f, -2.60561875e-04f, -1.41761141e-02f }, + { -2.22463426e-13f, -1.55078698e-08f, 3.81675160e-04f, -7.30646033e-01f }, + { 6.72595954e-13f, -2.73059993e-08f, 4.24068546e-04f, -7.52204323e-01f }, + }; + + if(t >= 12000.0f) + return make_float3(0.826270103f, 0.994478524f, 1.56626022f); + + /* Define a macro to reduce stack usage for nvcc */ +#define MAKE_BB_RGB(i) make_float3(\ + rc[i][0] / t + rc[i][1] * t + rc[i][2],\ + gc[i][0] / t + gc[i][1] * t + gc[i][2],\ + ((bc[i][0] * t + bc[i][1]) * t + bc[i][2]) * t + bc[i][3]) + + if(t >= 6365.0f) + return MAKE_BB_RGB(5); + if(t >= 3315.0f) + return MAKE_BB_RGB(4); + if(t >= 1902.0f) + return MAKE_BB_RGB(3); + if(t >= 1449.0f) + return MAKE_BB_RGB(2); + if(t >= 1167.0f) + return MAKE_BB_RGB(1); + if(t >= 965.0f) + return MAKE_BB_RGB(0); + +#undef MAKE_BB_RGB + + /* For 800 <= t < 965 color does not change in OSL implementation, so keep color the same */ + return make_float3(4.70366907f, 0.0f, 0.0f); +} + CCL_NAMESPACE_END |