Cycles: Subsurface Scattering
New features: * Bump mapping now works with SSS * Texture Blur factor for SSS, see the documentation for details: http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/Shaders#Subsurface_Scattering Work in progress for feedback: Initial implementation of the "BSSRDF Importance Sampling" paper, which uses a different importance sampling method. It gives better quality results in many ways, with the availability of both Cubic and Gaussian falloff functions, but also tends to be more noisy when using the progressive integrator and does not give great results with some geometry. It works quite well for the non-progressive integrator and is often less noisy there. This code may still change a lot, so unless you're testing it may be best to stick to the Compatible falloff function. Skin test render and file that takes advantage of the gaussian falloff: http://www.pasteall.org/pic/show.php?id=57661 http://www.pasteall.org/pic/show.php?id=57662 http://www.pasteall.org/blend/23501
This commit is contained in:
@@ -25,11 +25,16 @@
|
||||
#include "kernel_types.h"
|
||||
#include "kernel_montecarlo.h"
|
||||
|
||||
#include "closure/bsdf_diffuse.h"
|
||||
#include "closure/bssrdf.h"
|
||||
|
||||
CCL_NAMESPACE_BEGIN
|
||||
|
||||
static float bssrdf_cubic(float ld, float r)
|
||||
{
|
||||
if(ld == 0.0f)
|
||||
return (r == 0.0f)? 1.0f: 0.0f;
|
||||
|
||||
return powf(ld - min(r, ld), 3.0f) * 4.0f/powf(ld, 4.0f);
|
||||
}
|
||||
|
||||
/* Cumulative density function utilities */
|
||||
|
||||
static float cdf_lookup_inverse(const vector<float>& table, float2 range, float x)
|
||||
@@ -61,25 +66,19 @@ static void cdf_invert(vector<float>& to, float2 to_range, const vector<float>&
|
||||
|
||||
/* BSSRDF */
|
||||
|
||||
static float bssrdf_lookup_table_max_radius(const BSSRDFParams *ss)
|
||||
{
|
||||
/* todo: adjust when we use the real BSSRDF */
|
||||
return ss->ld;
|
||||
}
|
||||
|
||||
static void bssrdf_lookup_table_create(const BSSRDFParams *ss, vector<float>& sample_table, vector<float>& pdf_table)
|
||||
static void bssrdf_lookup_table_create(float ld, vector<float>& sample_table, vector<float>& pdf_table)
|
||||
{
|
||||
const int size = BSSRDF_RADIUS_TABLE_SIZE;
|
||||
vector<float> cdf(size);
|
||||
vector<float> pdf(size);
|
||||
float step = 1.0f/(float)(size - 1);
|
||||
float max_radius = bssrdf_lookup_table_max_radius(ss);
|
||||
float max_radius = ld;
|
||||
float pdf_sum = 0.0f;
|
||||
|
||||
/* compute the probability density function */
|
||||
for(int i = 0; i < pdf.size(); i++) {
|
||||
float x = (i*step)*max_radius;
|
||||
pdf[i] = bssrdf_cubic(ss->ld, x);
|
||||
pdf[i] = bssrdf_cubic(ld, x);
|
||||
pdf_sum += pdf[i];
|
||||
}
|
||||
|
||||
@@ -124,13 +123,9 @@ void bssrdf_table_build(vector<float>& table)
|
||||
|
||||
/* create a 2D lookup table, for reflection x sample radius */
|
||||
for(int i = 0; i < BSSRDF_REFL_TABLE_SIZE; i++) {
|
||||
float refl = (float)i/(float)(BSSRDF_REFL_TABLE_SIZE-1);
|
||||
float ior = 1.3f;
|
||||
float radius = 1.0f;
|
||||
|
||||
BSSRDFParams ss;
|
||||
bssrdf_setup_params(&ss, refl, radius, ior);
|
||||
bssrdf_lookup_table_create(&ss, sample_table, pdf_table);
|
||||
bssrdf_lookup_table_create(radius, sample_table, pdf_table);
|
||||
|
||||
memcpy(&table[i*BSSRDF_RADIUS_TABLE_SIZE], &sample_table[0], BSSRDF_RADIUS_TABLE_SIZE*sizeof(float));
|
||||
memcpy(&table[BSSRDF_PDF_TABLE_OFFSET + i*BSSRDF_RADIUS_TABLE_SIZE], &pdf_table[0], BSSRDF_RADIUS_TABLE_SIZE*sizeof(float));
|
||||
|
@@ -76,7 +76,8 @@ enum ShaderNodeSpecialType {
|
||||
SHADER_SPECIAL_TYPE_NONE,
|
||||
SHADER_SPECIAL_TYPE_PROXY,
|
||||
SHADER_SPECIAL_TYPE_MIX_CLOSURE,
|
||||
SHADER_SPECIAL_TYPE_AUTOCONVERT
|
||||
SHADER_SPECIAL_TYPE_AUTOCONVERT,
|
||||
SHADER_SPECIAL_TYPE_GEOMETRY
|
||||
};
|
||||
|
||||
/* Enum
|
||||
@@ -190,6 +191,7 @@ public:
|
||||
virtual bool has_surface_transparent() { return false; }
|
||||
virtual bool has_surface_bssrdf() { return false; }
|
||||
virtual bool has_converter_blackbody() { return false; }
|
||||
virtual bool has_bssrdf_bump() { return false; }
|
||||
|
||||
vector<ShaderInput*> inputs;
|
||||
vector<ShaderOutput*> outputs;
|
||||
|
@@ -1276,16 +1276,18 @@ void ProxyNode::compile(OSLCompiler& compiler)
|
||||
BsdfNode::BsdfNode(bool scattering_)
|
||||
: ShaderNode("bsdf"), scattering(scattering_)
|
||||
{
|
||||
closure = ccl::CLOSURE_BSSRDF_ID;
|
||||
|
||||
add_input("Color", SHADER_SOCKET_COLOR, make_float3(0.8f, 0.8f, 0.8f));
|
||||
add_input("Normal", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL);
|
||||
add_input("SurfaceMixWeight", SHADER_SOCKET_FLOAT, 0.0f, ShaderInput::USE_SVM);
|
||||
|
||||
if(scattering)
|
||||
if(scattering) {
|
||||
closure = CLOSURE_BSSRDF_CUBIC_ID;
|
||||
add_output("BSSRDF", SHADER_SOCKET_CLOSURE);
|
||||
else
|
||||
}
|
||||
else {
|
||||
closure = CLOSURE_BSDF_DIFFUSE_ID;
|
||||
add_output("BSDF", SHADER_SOCKET_CLOSURE);
|
||||
}
|
||||
}
|
||||
|
||||
void BsdfNode::compile(SVMCompiler& compiler, ShaderInput *param1, ShaderInput *param2, ShaderInput *param3)
|
||||
@@ -1600,27 +1602,47 @@ void TransparentBsdfNode::compile(OSLCompiler& compiler)
|
||||
|
||||
/* Subsurface Scattering Closure */
|
||||
|
||||
static ShaderEnum subsurface_falloff_init()
|
||||
{
|
||||
ShaderEnum enm;
|
||||
|
||||
enm.insert("Cubic", CLOSURE_BSSRDF_CUBIC_ID);
|
||||
enm.insert("Gaussian", CLOSURE_BSSRDF_GAUSSIAN_ID);
|
||||
|
||||
return enm;
|
||||
}
|
||||
|
||||
ShaderEnum SubsurfaceScatteringNode::falloff_enum = subsurface_falloff_init();
|
||||
|
||||
SubsurfaceScatteringNode::SubsurfaceScatteringNode()
|
||||
: BsdfNode(true)
|
||||
{
|
||||
name = "subsurface_scattering";
|
||||
closure = CLOSURE_BSSRDF_ID;
|
||||
closure = CLOSURE_BSSRDF_CUBIC_ID;
|
||||
|
||||
add_input("Scale", SHADER_SOCKET_FLOAT, 0.01f);
|
||||
add_input("Radius", SHADER_SOCKET_VECTOR, make_float3(0.1f, 0.1f, 0.1f));
|
||||
add_input("IOR", SHADER_SOCKET_FLOAT, 1.3f);
|
||||
add_input("Texture Blur", SHADER_SOCKET_FLOAT, 1.0f);
|
||||
}
|
||||
|
||||
void SubsurfaceScatteringNode::compile(SVMCompiler& compiler)
|
||||
{
|
||||
BsdfNode::compile(compiler, input("Scale"), input("IOR"), input("Radius"));
|
||||
BsdfNode::compile(compiler, input("Scale"), input("Texture Blur"), input("Radius"));
|
||||
}
|
||||
|
||||
void SubsurfaceScatteringNode::compile(OSLCompiler& compiler)
|
||||
{
|
||||
compiler.parameter("Falloff", falloff_enum[closure]);
|
||||
compiler.add(this, "node_subsurface_scattering");
|
||||
}
|
||||
|
||||
bool SubsurfaceScatteringNode::has_bssrdf_bump()
|
||||
{
|
||||
/* detect if anything is plugged into the normal input besides the default */
|
||||
ShaderInput *normal_in = input("Normal");
|
||||
return (normal_in->link && normal_in->link->parent->special_type != SHADER_SPECIAL_TYPE_GEOMETRY);
|
||||
}
|
||||
|
||||
/* Emissive Closure */
|
||||
|
||||
EmissionNode::EmissionNode()
|
||||
@@ -1835,6 +1857,8 @@ void IsotropicVolumeNode::compile(OSLCompiler& compiler)
|
||||
GeometryNode::GeometryNode()
|
||||
: ShaderNode("geometry")
|
||||
{
|
||||
special_type = SHADER_SPECIAL_TYPE_GEOMETRY;
|
||||
|
||||
add_input("NormalIn", SHADER_SOCKET_NORMAL, ShaderInput::NORMAL, ShaderInput::USE_OSL);
|
||||
add_output("Position", SHADER_SOCKET_POINT);
|
||||
add_output("Normal", SHADER_SOCKET_NORMAL);
|
||||
|
@@ -271,6 +271,9 @@ class SubsurfaceScatteringNode : public BsdfNode {
|
||||
public:
|
||||
SHADER_NODE_CLASS(SubsurfaceScatteringNode)
|
||||
bool has_surface_bssrdf() { return true; }
|
||||
bool has_bssrdf_bump();
|
||||
|
||||
static ShaderEnum falloff_enum;
|
||||
};
|
||||
|
||||
class EmissionNode : public ShaderNode {
|
||||
|
@@ -201,11 +201,16 @@ void OSLShaderManager::shading_system_init()
|
||||
"reflection", /* PATH_RAY_REFLECT */
|
||||
"refraction", /* PATH_RAY_TRANSMIT */
|
||||
"diffuse", /* PATH_RAY_DIFFUSE */
|
||||
"gloss_sharedy", /* PATH_RAY_GLOSSY */
|
||||
"glossy", /* PATH_RAY_GLOSSY */
|
||||
"singular", /* PATH_RAY_SINGULAR */
|
||||
"transparent", /* PATH_RAY_TRANSPARENT */
|
||||
"shadow", /* PATH_RAY_SHADOW_OPAQUE */
|
||||
"shadow", /* PATH_RAY_SHADOW_TRANSPARENT */
|
||||
|
||||
"__unused__",
|
||||
"__unused__",
|
||||
"diffuse_ancestor", /* PATH_RAY_DIFFUSE_ANCESTOR */
|
||||
"glossy_ancestor", /* PATH_RAY_GLOSSY_ANCESTOR */
|
||||
};
|
||||
|
||||
const int nraytypes = sizeof(raytypes)/sizeof(raytypes[0]);
|
||||
@@ -543,8 +548,10 @@ void OSLCompiler::add(ShaderNode *node, const char *name, bool isfilepath)
|
||||
current_shader->has_surface_emission = true;
|
||||
if(info->has_surface_transparent)
|
||||
current_shader->has_surface_transparent = true;
|
||||
if(info->has_surface_bssrdf)
|
||||
if(info->has_surface_bssrdf) {
|
||||
current_shader->has_surface_bssrdf = true;
|
||||
current_shader->has_bssrdf_bump = true; /* can't detect yet */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -705,8 +712,11 @@ void OSLCompiler::generate_nodes(const set<ShaderNode*>& nodes)
|
||||
current_shader->has_surface_emission = true;
|
||||
if(node->has_surface_transparent())
|
||||
current_shader->has_surface_transparent = true;
|
||||
if(node->has_surface_bssrdf())
|
||||
if(node->has_surface_bssrdf()) {
|
||||
current_shader->has_surface_bssrdf = true;
|
||||
if(node->has_bssrdf_bump())
|
||||
current_shader->has_bssrdf_bump = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
nodes_done = false;
|
||||
@@ -773,6 +783,7 @@ void OSLCompiler::compile(OSLGlobals *og, Shader *shader)
|
||||
shader->has_surface_emission = false;
|
||||
shader->has_surface_transparent = false;
|
||||
shader->has_surface_bssrdf = false;
|
||||
shader->has_bssrdf_bump = false;
|
||||
shader->has_volume = false;
|
||||
shader->has_displacement = false;
|
||||
|
||||
|
@@ -55,6 +55,7 @@ Shader::Shader()
|
||||
has_converter_blackbody = false;
|
||||
has_volume = false;
|
||||
has_displacement = false;
|
||||
has_bssrdf_bump = false;
|
||||
|
||||
used = false;
|
||||
|
||||
@@ -236,11 +237,19 @@ 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_bssrdf_bump)
|
||||
flag |= SD_HAS_BSSRDF_BUMP;
|
||||
if(shader->has_converter_blackbody)
|
||||
has_converter_blackbody = true;
|
||||
|
||||
/* regular shader */
|
||||
shader_flag[i++] = flag;
|
||||
shader_flag[i++] = shader->pass_id;
|
||||
|
||||
/* shader with bump mapping */
|
||||
if(shader->graph_bump)
|
||||
flag |= SD_HAS_BSSRDF_BUMP;
|
||||
|
||||
shader_flag[i++] = flag;
|
||||
shader_flag[i++] = shader->pass_id;
|
||||
}
|
||||
|
@@ -78,6 +78,7 @@ public:
|
||||
bool has_displacement;
|
||||
bool has_surface_bssrdf;
|
||||
bool has_converter_blackbody;
|
||||
bool has_bssrdf_bump;
|
||||
|
||||
/* requested mesh attributes */
|
||||
AttributeRequestSet attributes;
|
||||
|
@@ -495,8 +495,11 @@ void SVMCompiler::generate_closure(ShaderNode *node, set<ShaderNode*>& done)
|
||||
current_shader->has_surface_emission = true;
|
||||
if(node->has_surface_transparent())
|
||||
current_shader->has_surface_transparent = true;
|
||||
if(node->has_surface_bssrdf())
|
||||
if(node->has_surface_bssrdf()) {
|
||||
current_shader->has_surface_bssrdf = true;
|
||||
if(node->has_bssrdf_bump())
|
||||
current_shader->has_bssrdf_bump = true;
|
||||
}
|
||||
|
||||
/* end node is added outside of this */
|
||||
}
|
||||
@@ -557,8 +560,11 @@ void SVMCompiler::generate_multi_closure(ShaderNode *node, set<ShaderNode*>& don
|
||||
current_shader->has_surface_emission = true;
|
||||
if(node->has_surface_transparent())
|
||||
current_shader->has_surface_transparent = true;
|
||||
if(node->has_surface_bssrdf())
|
||||
if(node->has_surface_bssrdf()) {
|
||||
current_shader->has_surface_bssrdf = true;
|
||||
if(node->has_bssrdf_bump())
|
||||
current_shader->has_bssrdf_bump = true;
|
||||
}
|
||||
}
|
||||
|
||||
done.insert(node);
|
||||
@@ -676,6 +682,7 @@ void SVMCompiler::compile(Shader *shader, vector<int4>& global_svm_nodes, int in
|
||||
shader->has_surface_emission = false;
|
||||
shader->has_surface_transparent = false;
|
||||
shader->has_surface_bssrdf = false;
|
||||
shader->has_bssrdf_bump = false;
|
||||
shader->has_converter_blackbody = false;
|
||||
shader->has_volume = false;
|
||||
shader->has_displacement = false;
|
||||
|
Reference in New Issue
Block a user