From 6d9720ef63c540d533207aded1114da862377d32 Mon Sep 17 00:00:00 2001 From: Thomas Dinges Date: Wed, 31 Jul 2013 20:56:32 +0000 Subject: [PATCH] Cycles / Blackbody to RGB node: * Added a node to convert a temperature in Kelvin to an RGB color. This can be used e.g. for lights, to easily find the right color temperature. = Some common temperatures = Candle light: 1500 Kelvin Sunset/Sunrise: 1850 Kelvin Studio lamps: 3200 Kelvin Horizon daylight: 5000 Kelvin Documentation: http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/More#Blackbody Thanks to Philipp Oeser (lichtwerk), who essentially contributed to this with a patch! :) This is part of my GSoC 2013 project. SVN merge of r57424, r57487, r57507, r57525, r58253 and r58774 --- intern/cycles/blender/blender_shader.cpp | 3 + intern/cycles/kernel/CMakeLists.txt | 1 + intern/cycles/kernel/kernel_types.h | 13 ++ intern/cycles/kernel/shaders/CMakeLists.txt | 1 + .../cycles/kernel/shaders/node_blackbody.osl | 33 +++++ intern/cycles/kernel/svm/svm.h | 4 + intern/cycles/kernel/svm/svm_blackbody.h | 84 +++++++++++ intern/cycles/kernel/svm/svm_types.h | 1 + intern/cycles/render/CMakeLists.txt | 2 + intern/cycles/render/blackbody.cpp | 140 ++++++++++++++++++ intern/cycles/render/blackbody.h | 30 ++++ intern/cycles/render/graph.h | 1 + intern/cycles/render/nodes.cpp | 24 +++ intern/cycles/render/nodes.h | 7 + intern/cycles/render/shader.cpp | 26 ++++ intern/cycles/render/shader.h | 2 + intern/cycles/render/svm.cpp | 5 + release/scripts/startup/nodeitems_builtins.py | 1 + source/blender/blenkernel/BKE_node.h | 1 + source/blender/blenkernel/intern/node.c | 1 + source/blender/nodes/CMakeLists.txt | 1 + source/blender/nodes/NOD_shader.h | 1 + source/blender/nodes/NOD_static_types.h | 1 + .../shader/nodes/node_shader_blackbody.c | 54 +++++++ 24 files changed, 437 insertions(+) create mode 100644 intern/cycles/kernel/shaders/node_blackbody.osl create mode 100644 intern/cycles/kernel/svm/svm_blackbody.h create mode 100644 intern/cycles/render/blackbody.cpp create mode 100644 intern/cycles/render/blackbody.h create mode 100644 source/blender/nodes/shader/nodes/node_shader_blackbody.c diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp index 923f9b1e726..ee6a0887d45 100644 --- a/intern/cycles/blender/blender_shader.cpp +++ b/intern/cycles/blender/blender_shader.cpp @@ -400,6 +400,9 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen else if (b_node.is_a(&RNA_ShaderNodeWavelength)) { node = new WavelengthNode(); } + else if (b_node.is_a(&RNA_ShaderNodeBlackbody)) { + node = new BlackbodyNode(); + } else if (b_node.is_a(&RNA_ShaderNodeLightPath)) { node = new LightPathNode(); } diff --git a/intern/cycles/kernel/CMakeLists.txt b/intern/cycles/kernel/CMakeLists.txt index 2fa1393e935..ac0b9895c78 100644 --- a/intern/cycles/kernel/CMakeLists.txt +++ b/intern/cycles/kernel/CMakeLists.txt @@ -72,6 +72,7 @@ set(SRC_CLOSURE_HEADERS set(SRC_SVM_HEADERS svm/svm.h svm/svm_attribute.h + svm/svm_blackbody.h svm/svm_camera.h svm/svm_closure.h svm/svm_convert.h diff --git a/intern/cycles/kernel/kernel_types.h b/intern/cycles/kernel/kernel_types.h index 733eb665860..1010fdea627 100644 --- a/intern/cycles/kernel/kernel_types.h +++ b/intern/cycles/kernel/kernel_types.h @@ -44,6 +44,12 @@ CCL_NAMESPACE_BEGIN #define BSSRDF_MIN_RADIUS 1e-8f #define BSSRDF_MAX_ATTEMPTS 8 +#define BB_DRAPPER 800.0 +#define BB_MAX_TABLE_RANGE 12000.0 +#define BB_TABLE_XPOWER 1.5 +#define BB_TABLE_YPOWER 5.0 +#define BB_TABLE_SPACING 2.0 + #define TEX_NUM_FLOAT_IMAGES 5 /* device capabilities */ @@ -810,6 +816,12 @@ typedef struct KernelBSSRDF { int pad1, pad2; } KernelBSSRDF; +typedef struct KernelBlackbody { + int table_offset; + int pad1, pad2, pad3; +} KernelBLACKBODY; + + typedef struct KernelData { KernelCamera cam; KernelFilm film; @@ -819,6 +831,7 @@ typedef struct KernelData { KernelBVH bvh; KernelCurves curve_kernel_data; KernelBSSRDF bssrdf; + KernelBlackbody blackbody; } KernelData; CCL_NAMESPACE_END diff --git a/intern/cycles/kernel/shaders/CMakeLists.txt b/intern/cycles/kernel/shaders/CMakeLists.txt index 23112f56536..2df8140ebbd 100644 --- a/intern/cycles/kernel/shaders/CMakeLists.txt +++ b/intern/cycles/kernel/shaders/CMakeLists.txt @@ -68,6 +68,7 @@ set(SRC_OSL node_voronoi_texture.osl node_ward_bsdf.osl node_wavelength.osl + node_blackbody.osl node_wave_texture.osl node_wireframe.osl ) diff --git a/intern/cycles/kernel/shaders/node_blackbody.osl b/intern/cycles/kernel/shaders/node_blackbody.osl new file mode 100644 index 00000000000..3eb0ec4b5e3 --- /dev/null +++ b/intern/cycles/kernel/shaders/node_blackbody.osl @@ -0,0 +1,33 @@ +/* + * Copyright 2013, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "stdosl.h" + +shader node_blackbody( + float Temperature = 1200.0, + output color Color = 0.0) +{ + color rgb = blackbody(Temperature); + + /* Scale by luminance */ + float l = luminance(rgb); + if (l != 0.0) + rgb /= l; + Color = rgb; +} + diff --git a/intern/cycles/kernel/svm/svm.h b/intern/cycles/kernel/svm/svm.h index e5df880b596..0a8896f3c44 100644 --- a/intern/cycles/kernel/svm/svm.h +++ b/intern/cycles/kernel/svm/svm.h @@ -146,6 +146,7 @@ CCL_NAMESPACE_END #include "svm_attribute.h" #include "svm_gradient.h" +#include "svm_blackbody.h" #include "svm_closure.h" #include "svm_noisetex.h" #include "svm_convert.h" @@ -366,6 +367,9 @@ __device_noinline void svm_eval_nodes(KernelGlobals *kg, ShaderData *sd, ShaderT case NODE_WAVELENGTH: svm_node_wavelength(sd, stack, node.y, node.z); break; + case NODE_BLACKBODY: + svm_node_blackbody(kg, sd, stack, node.y, node.z); + break; case NODE_SET_DISPLACEMENT: svm_node_set_displacement(sd, stack, node.y); break; diff --git a/intern/cycles/kernel/svm/svm_blackbody.h b/intern/cycles/kernel/svm/svm_blackbody.h new file mode 100644 index 00000000000..3c6e11ca683 --- /dev/null +++ b/intern/cycles/kernel/svm/svm_blackbody.h @@ -0,0 +1,84 @@ +/* + * Adapted from Open Shading Language with this license: + * + * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al. + * All Rights Reserved. + * + * Modifications Copyright 2013, Blender Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Sony Pictures Imageworks nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +CCL_NAMESPACE_BEGIN + +/* Blackbody Node */ + +__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_DRAPPER) { + /* 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_DRAPPER) * (1.0f / BB_TABLE_SPACING), 1.0f/BB_TABLE_XPOWER); + + int blackbody_table_offset = kernel_data.blackbody.table_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; + + if (stack_valid(col_offset)) + stack_store_float3(stack, col_offset, color_rgb); +} + +CCL_NAMESPACE_END diff --git a/intern/cycles/kernel/svm/svm_types.h b/intern/cycles/kernel/svm/svm_types.h index 85719265292..1e096c7f76c 100644 --- a/intern/cycles/kernel/svm/svm_types.h +++ b/intern/cycles/kernel/svm/svm_types.h @@ -53,6 +53,7 @@ typedef enum NodeType { NODE_FRESNEL, NODE_WIREFRAME, NODE_WAVELENGTH, + NODE_BLACKBODY, NODE_EMISSION_WEIGHT, NODE_TEX_GRADIENT, NODE_TEX_VORONOI, diff --git a/intern/cycles/render/CMakeLists.txt b/intern/cycles/render/CMakeLists.txt index f3f230c29dc..0cf29435a8c 100644 --- a/intern/cycles/render/CMakeLists.txt +++ b/intern/cycles/render/CMakeLists.txt @@ -16,6 +16,7 @@ set(INC_SYS set(SRC attribute.cpp background.cpp + blackbody.cpp buffers.cpp bssrdf.cpp camera.cpp @@ -43,6 +44,7 @@ set(SRC set(SRC_HEADERS attribute.h background.h + blackbody.h buffers.h bssrdf.h camera.h diff --git a/intern/cycles/render/blackbody.cpp b/intern/cycles/render/blackbody.cpp new file mode 100644 index 00000000000..467368935d7 --- /dev/null +++ b/intern/cycles/render/blackbody.cpp @@ -0,0 +1,140 @@ +/* + * Adapted from Open Shading Language with this license: + * + * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al. + * All Rights Reserved. + * + * Modifications Copyright 2013, Blender Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Sony Pictures Imageworks nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "blackbody.h" +#include "util_color.h" +#include "util_math.h" + +#include "kernel_types.h" + +CCL_NAMESPACE_BEGIN + +vector blackbody_table() +{ + /* quoted from OSLs opcolor.cpp + In order to speed up the blackbody computation, we have a table + storing the precomputed BB values for a range of temperatures. Less + than BB_DRAPER always returns 0. Greater than BB_MAX_TABLE_RANGE + does the full computation, we think it'll be rare to inquire higher + temperatures. + + Since the bb function is so nonlinear, we actually space the table + entries nonlinearly, with the relationship between the table index i + and the temperature T as follows: + i = ((T-Draper)/spacing)^(1/xpower) + T = pow(i, xpower) * spacing + Draper + And furthermore, we store in the table the true value raised ^(1/5). + I tuned this a bit, and with the current values we can have all + blackbody results accurate to within 0.1% with a table size of 317 + (about 5 KB of data). + */ + + const float cie_colour_match[81][3] = { + {0.0014,0.0000,0.0065}, {0.0022,0.0001,0.0105}, {0.0042,0.0001,0.0201}, + {0.0076,0.0002,0.0362}, {0.0143,0.0004,0.0679}, {0.0232,0.0006,0.1102}, + {0.0435,0.0012,0.2074}, {0.0776,0.0022,0.3713}, {0.1344,0.0040,0.6456}, + {0.2148,0.0073,1.0391}, {0.2839,0.0116,1.3856}, {0.3285,0.0168,1.6230}, + {0.3483,0.0230,1.7471}, {0.3481,0.0298,1.7826}, {0.3362,0.0380,1.7721}, + {0.3187,0.0480,1.7441}, {0.2908,0.0600,1.6692}, {0.2511,0.0739,1.5281}, + {0.1954,0.0910,1.2876}, {0.1421,0.1126,1.0419}, {0.0956,0.1390,0.8130}, + {0.0580,0.1693,0.6162}, {0.0320,0.2080,0.4652}, {0.0147,0.2586,0.3533}, + {0.0049,0.3230,0.2720}, {0.0024,0.4073,0.2123}, {0.0093,0.5030,0.1582}, + {0.0291,0.6082,0.1117}, {0.0633,0.7100,0.0782}, {0.1096,0.7932,0.0573}, + {0.1655,0.8620,0.0422}, {0.2257,0.9149,0.0298}, {0.2904,0.9540,0.0203}, + {0.3597,0.9803,0.0134}, {0.4334,0.9950,0.0087}, {0.5121,1.0000,0.0057}, + {0.5945,0.9950,0.0039}, {0.6784,0.9786,0.0027}, {0.7621,0.9520,0.0021}, + {0.8425,0.9154,0.0018}, {0.9163,0.8700,0.0017}, {0.9786,0.8163,0.0014}, + {1.0263,0.7570,0.0011}, {1.0567,0.6949,0.0010}, {1.0622,0.6310,0.0008}, + {1.0456,0.5668,0.0006}, {1.0026,0.5030,0.0003}, {0.9384,0.4412,0.0002}, + {0.8544,0.3810,0.0002}, {0.7514,0.3210,0.0001}, {0.6424,0.2650,0.0000}, + {0.5419,0.2170,0.0000}, {0.4479,0.1750,0.0000}, {0.3608,0.1382,0.0000}, + {0.2835,0.1070,0.0000}, {0.2187,0.0816,0.0000}, {0.1649,0.0610,0.0000}, + {0.1212,0.0446,0.0000}, {0.0874,0.0320,0.0000}, {0.0636,0.0232,0.0000}, + {0.0468,0.0170,0.0000}, {0.0329,0.0119,0.0000}, {0.0227,0.0082,0.0000}, + {0.0158,0.0057,0.0000}, {0.0114,0.0041,0.0000}, {0.0081,0.0029,0.0000}, + {0.0058,0.0021,0.0000}, {0.0041,0.0015,0.0000}, {0.0029,0.0010,0.0000}, + {0.0020,0.0007,0.0000}, {0.0014,0.0005,0.0000}, {0.0010,0.0004,0.0000}, + {0.0007,0.0002,0.0000}, {0.0005,0.0002,0.0000}, {0.0003,0.0001,0.0000}, + {0.0002,0.0001,0.0000}, {0.0002,0.0001,0.0000}, {0.0001,0.0000,0.0000}, + {0.0001,0.0000,0.0000}, {0.0001,0.0000,0.0000}, {0.0000,0.0000,0.0000} + }; + + const double c1 = 3.74183e-16; // 2*pi*h*c^2, W*m^2 + const double c2 = 1.4388e-2; // h*c/k, m*K + // h is Planck's const, k is Boltzmann's + const float dlambda = 5.0f * 1e-9; // in meters + + /* Blackbody table from 800 to 12k Kelvin (319 entries (317+2 offset) * 3) */ + vector blackbody_table(956); + + float X, Y, Z; + + /* ToDo: bring this back to what OSL does with the lastTemperature limit ? */ + for (int i = 0; i <= 317; ++i) { + float Temperature = powf (float(i), BB_TABLE_XPOWER) * BB_TABLE_SPACING + BB_DRAPPER; + X = 0; + Y = 0; + Z = 0; + + /* from OSL "spectrum_to_XYZ" */ + for (int n = 0; n < 81; ++n) { + float lambda = 380.0f + 5.0f * n; + double wlm = lambda * 1e-9; // Wavelength in meters + // N.B. spec_intens returns result in W/m^2 but it's a differential, + // needs to be scaled by dlambda! + float spec_intens = float((c1 * powf(wlm,-5.0)) / (expf(c2 / (wlm * Temperature)) -1.0f)); + float Me = spec_intens * dlambda; + + X += Me * cie_colour_match[n][0]; + Y += Me * cie_colour_match[n][1]; + Z += Me * cie_colour_match[n][2]; + } + + /* Convert from xyz color space */ + float3 col = xyz_to_rgb(X, Y, Z); + + /* Clamp to zero if values are smaller */ + col = max(col, make_float3(0.0f, 0.0f, 0.0f)); + + col.x = powf(col.x, 1.0f / BB_TABLE_YPOWER); + col.y = powf(col.y, 1.0f / BB_TABLE_YPOWER); + col.z = powf(col.z, 1.0f / BB_TABLE_YPOWER); + + /* Store in table in RRRGGGBBB format */ + blackbody_table[i] = col.x; + blackbody_table[i+319*1] = col.y; + blackbody_table[i+319*2] = col.z; + } + + return blackbody_table; +} +CCL_NAMESPACE_END diff --git a/intern/cycles/render/blackbody.h b/intern/cycles/render/blackbody.h new file mode 100644 index 00000000000..a177238f689 --- /dev/null +++ b/intern/cycles/render/blackbody.h @@ -0,0 +1,30 @@ +/* + * Copyright 2011, Blender Foundation. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __BLACKBODY_H__ +#define __BLACKBODY_H__ + +#include "util_vector.h" + +CCL_NAMESPACE_BEGIN + +vector blackbody_table(); + +CCL_NAMESPACE_END + +#endif /* __BLACKBODY_H__ */ diff --git a/intern/cycles/render/graph.h b/intern/cycles/render/graph.h index 90e4760ba1c..da8ed987346 100644 --- a/intern/cycles/render/graph.h +++ b/intern/cycles/render/graph.h @@ -189,6 +189,7 @@ public: virtual bool has_surface_emission() { return false; } virtual bool has_surface_transparent() { return false; } virtual bool has_surface_bssrdf() { return false; } + virtual bool has_converter_blackbody() { return false; } vector inputs; vector outputs; diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index bd254c8e0d1..9349e7b90ae 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -2993,6 +2993,30 @@ void WavelengthNode::compile(OSLCompiler& compiler) compiler.add(this, "node_wavelength"); } +/* Blackbody */ + +BlackbodyNode::BlackbodyNode() +: ShaderNode("Blackbody") +{ + add_input("Temperature", SHADER_SOCKET_FLOAT, 1200.0f); + add_output("Color", SHADER_SOCKET_COLOR); +} + +void BlackbodyNode::compile(SVMCompiler& compiler) +{ + ShaderInput *temperature_in = input("Temperature"); + ShaderOutput *color_out = output("Color"); + + compiler.stack_assign(temperature_in); + compiler.stack_assign(color_out); + compiler.add_node(NODE_BLACKBODY, temperature_in->stack_offset, color_out->stack_offset); +} + +void BlackbodyNode::compile(OSLCompiler& compiler) +{ + compiler.add(this, "node_blackbody"); +} + /* Output */ OutputNode::OutputNode() diff --git a/intern/cycles/render/nodes.h b/intern/cycles/render/nodes.h index 78920d589ed..532ff41abee 100644 --- a/intern/cycles/render/nodes.h +++ b/intern/cycles/render/nodes.h @@ -462,6 +462,13 @@ public: SHADER_NODE_CLASS(WavelengthNode) }; +class BlackbodyNode : public ShaderNode { +public: + SHADER_NODE_CLASS(BlackbodyNode) + + bool has_converter_blackbody() { return true; } +}; + class MathNode : public ShaderNode { public: SHADER_NODE_CLASS(MathNode) diff --git a/intern/cycles/render/shader.cpp b/intern/cycles/render/shader.cpp index 21b43380f2c..5b326e0a017 100644 --- a/intern/cycles/render/shader.cpp +++ b/intern/cycles/render/shader.cpp @@ -18,6 +18,7 @@ #include "background.h" #include "bssrdf.h" +#include "blackbody.h" #include "device.h" #include "graph.h" #include "light.h" @@ -51,6 +52,7 @@ Shader::Shader() has_surface_transparent = false; has_surface_emission = false; has_surface_bssrdf = false; + has_converter_blackbody = false; has_volume = false; has_displacement = false; @@ -127,6 +129,7 @@ ShaderManager::ShaderManager() { need_update = true; bssrdf_table_offset = TABLE_OFFSET_INVALID; + blackbody_table_offset = TABLE_OFFSET_INVALID; } ShaderManager::~ShaderManager() @@ -218,6 +221,7 @@ void ShaderManager::device_update_common(Device *device, DeviceScene *dscene, Sc uint *shader_flag = dscene->shader_flag.resize(shader_flag_size); uint i = 0; bool has_surface_bssrdf = false; + bool has_converter_blackbody = false; foreach(Shader *shader, scene->shaders) { uint flag = 0; @@ -232,6 +236,8 @@ void ShaderManager::device_update_common(Device *device, DeviceScene *dscene, Sc flag |= SD_HOMOGENEOUS_VOLUME; if(shader->has_surface_bssrdf) has_surface_bssrdf = true; + if(shader->has_converter_blackbody) + has_converter_blackbody = true; shader_flag[i++] = flag; shader_flag[i++] = shader->pass_id; @@ -257,6 +263,21 @@ void ShaderManager::device_update_common(Device *device, DeviceScene *dscene, Sc scene->lookup_tables->remove_table(bssrdf_table_offset); bssrdf_table_offset = TABLE_OFFSET_INVALID; } + + /* blackbody lookup table */ + KernelBlackbody *kblackbody = &dscene->data.blackbody; + + if(has_converter_blackbody && blackbody_table_offset == TABLE_OFFSET_INVALID) { + vector table = blackbody_table(); + blackbody_table_offset = scene->lookup_tables->add_table(dscene, table); + + kblackbody->table_offset = (int)blackbody_table_offset; + } + else if(!has_converter_blackbody && blackbody_table_offset != TABLE_OFFSET_INVALID) { + scene->lookup_tables->remove_table(blackbody_table_offset); + blackbody_table_offset = TABLE_OFFSET_INVALID; + } + } void ShaderManager::device_free_common(Device *device, DeviceScene *dscene, Scene *scene) @@ -266,6 +287,11 @@ void ShaderManager::device_free_common(Device *device, DeviceScene *dscene, Scen bssrdf_table_offset = TABLE_OFFSET_INVALID; } + if(blackbody_table_offset != TABLE_OFFSET_INVALID) { + scene->lookup_tables->remove_table(blackbody_table_offset); + blackbody_table_offset = TABLE_OFFSET_INVALID; + } + device->tex_free(dscene->shader_flag); dscene->shader_flag.clear(); } diff --git a/intern/cycles/render/shader.h b/intern/cycles/render/shader.h index 8810017068b..d7eac603fa6 100644 --- a/intern/cycles/render/shader.h +++ b/intern/cycles/render/shader.h @@ -77,6 +77,7 @@ public: bool has_volume; bool has_displacement; bool has_surface_bssrdf; + bool has_converter_blackbody; /* requested mesh attributes */ AttributeRequestSet attributes; @@ -142,6 +143,7 @@ protected: AttributeIDMap unique_attribute_id; size_t bssrdf_table_offset; + size_t blackbody_table_offset; }; CCL_NAMESPACE_END diff --git a/intern/cycles/render/svm.cpp b/intern/cycles/render/svm.cpp index 3e211a3ee24..9e887aeadf2 100644 --- a/intern/cycles/render/svm.cpp +++ b/intern/cycles/render/svm.cpp @@ -398,6 +398,10 @@ void SVMCompiler::generate_svm_nodes(const set& nodes, sethas_converter_blackbody()) + current_shader->has_converter_blackbody = true; + node->compile(*this); stack_clear_users(node, done); stack_clear_temporary(node); @@ -672,6 +676,7 @@ void SVMCompiler::compile(Shader *shader, vector& global_svm_nodes, int in shader->has_surface_emission = false; shader->has_surface_transparent = false; shader->has_surface_bssrdf = false; + shader->has_converter_blackbody = false; shader->has_volume = false; shader->has_displacement = false; diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py index af966fa106e..f4bfc9b0cec 100644 --- a/release/scripts/startup/nodeitems_builtins.py +++ b/release/scripts/startup/nodeitems_builtins.py @@ -227,6 +227,7 @@ shader_node_categories = [ NodeItem("ShaderNodeSeparateRGB"), NodeItem("ShaderNodeCombineRGB"), NodeItem("ShaderNodeWavelength"), + NodeItem("ShaderNodeBlackbody"), ]), ShaderNewNodeCategory("SH_NEW_SCRIPT", "Script", items=[ NodeItem("ShaderNodeScript"), diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index b43747ab33a..bc1ef7ac5c9 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -742,6 +742,7 @@ struct ShadeResult; #define SH_NODE_WIREFRAME 178 #define SH_NODE_BSDF_TOON 179 #define SH_NODE_WAVELENGTH 180 +#define SH_NODE_BLACKBODY 181 /* custom defines options for Material node */ #define SH_NODE_MAT_DIFF 1 diff --git a/source/blender/blenkernel/intern/node.c b/source/blender/blenkernel/intern/node.c index 84dfa70abfc..0cebbf03203 100644 --- a/source/blender/blenkernel/intern/node.c +++ b/source/blender/blenkernel/intern/node.c @@ -3406,6 +3406,7 @@ static void registerShaderNodes(void) register_node_type_sh_rgb(); register_node_type_sh_wireframe(); register_node_type_sh_wavelength(); + register_node_type_sh_blackbody(); register_node_type_sh_mix_rgb(); register_node_type_sh_valtorgb(); register_node_type_sh_rgbtobw(); diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt index cc1a5f8cee9..4b7ee2aed51 100644 --- a/source/blender/nodes/CMakeLists.txt +++ b/source/blender/nodes/CMakeLists.txt @@ -146,6 +146,7 @@ set(SRC shader/nodes/node_shader_value.c shader/nodes/node_shader_wireframe.c shader/nodes/node_shader_wavelength.c + shader/nodes/node_shader_blackbody.c shader/nodes/node_shader_vectMath.c shader/nodes/node_shader_add_shader.c shader/nodes/node_shader_ambient_occlusion.c diff --git a/source/blender/nodes/NOD_shader.h b/source/blender/nodes/NOD_shader.h index edb0ba6f475..a56d78af309 100644 --- a/source/blender/nodes/NOD_shader.h +++ b/source/blender/nodes/NOD_shader.h @@ -81,6 +81,7 @@ void register_node_type_sh_object_info(void); void register_node_type_sh_fresnel(void); void register_node_type_sh_wireframe(void); void register_node_type_sh_wavelength(void); +void register_node_type_sh_blackbody(void); void register_node_type_sh_layer_weight(void); void register_node_type_sh_tex_coord(void); void register_node_type_sh_particle_info(void); diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h index 69f2a0ae955..a7728cc0dde 100644 --- a/source/blender/nodes/NOD_static_types.h +++ b/source/blender/nodes/NOD_static_types.h @@ -97,6 +97,7 @@ DefNode( ShaderNode, SH_NODE_PARTICLE_INFO, 0, "PA DefNode( ShaderNode, SH_NODE_HAIR_INFO, 0, "HAIR_INFO", HairInfo, "Hair Info", "" ) DefNode( ShaderNode, SH_NODE_WIREFRAME, def_sh_tex_wireframe, "WIREFRAME", Wireframe, "Wireframe", "" ) DefNode( ShaderNode, SH_NODE_WAVELENGTH, 0, "WAVELENGTH", Wavelength, "Wavelength", "" ) +DefNode( ShaderNode, SH_NODE_BLACKBODY, 0, "BLACKBODY", Blackbody, "Blackbody", "" ) DefNode( ShaderNode, SH_NODE_BUMP, def_sh_bump, "BUMP", Bump, "Bump", "" ) DefNode( ShaderNode, SH_NODE_NORMAL_MAP, def_sh_normal_map, "NORMAL_MAP", NormalMap, "Normal Map", "" ) DefNode( ShaderNode, SH_NODE_TANGENT, def_sh_tangent, "TANGENT", Tangent, "Tangent", "" ) diff --git a/source/blender/nodes/shader/nodes/node_shader_blackbody.c b/source/blender/nodes/shader/nodes/node_shader_blackbody.c new file mode 100644 index 00000000000..af89a959554 --- /dev/null +++ b/source/blender/nodes/shader/nodes/node_shader_blackbody.c @@ -0,0 +1,54 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2005 Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +#include "../node_shader_util.h" + +/* **************** Blackbody ******************** */ +static bNodeSocketTemplate sh_node_blackbody_in[] = { + { SOCK_FLOAT, 1, N_("Temperature"), 1500.0f, 0.0f, 0.0f, 0.0f, 800.0f, 12000.0f}, + { -1, 0, "" } +}; + +static bNodeSocketTemplate sh_node_blackbody_out[] = { + { SOCK_RGBA, 0, N_("Color")}, + { -1, 0, "" } +}; + +/* node type definition */ +void register_node_type_sh_blackbody(void) +{ + static bNodeType ntype; + + sh_node_type_base(&ntype, SH_NODE_BLACKBODY, "Blackbody", NODE_CLASS_CONVERTOR, 0); + node_type_compatibility(&ntype, NODE_NEW_SHADING); + node_type_size_preset(&ntype, NODE_SIZE_MIDDLE); + node_type_socket_templates(&ntype, sh_node_blackbody_in, sh_node_blackbody_out); + node_type_init(&ntype, NULL); + node_type_storage(&ntype, "", NULL, NULL); + + nodeRegisterType(&ntype); +}