Code refactor: make mixed small/large BSSRDF radii more robust.

This commit is contained in:
Brecht Van Lommel
2018-01-26 22:11:28 +01:00
parent d611cf9233
commit 7b29e91711
3 changed files with 116 additions and 105 deletions

View File

@@ -27,6 +27,7 @@ typedef ccl_addr_space struct Bssrdf {
float sharpness; float sharpness;
float texture_blur; float texture_blur;
float roughness; float roughness;
float channels;
} Bssrdf; } Bssrdf;
/* Planar Truncated Gaussian /* Planar Truncated Gaussian
@@ -343,42 +344,68 @@ ccl_device_inline Bssrdf *bssrdf_alloc(ShaderData *sd, float3 weight)
return (sample_weight >= CLOSURE_WEIGHT_CUTOFF) ? bssrdf : NULL; return (sample_weight >= CLOSURE_WEIGHT_CUTOFF) ? bssrdf : NULL;
} }
ccl_device int bssrdf_setup(Bssrdf *bssrdf, ClosureType type) ccl_device int bssrdf_setup(ShaderData *sd, Bssrdf *bssrdf, ClosureType type)
{ {
if(max3(bssrdf->radius) < BSSRDF_MIN_RADIUS) { int flag = 0;
/* revert to diffuse BSDF if radius too small */ int bssrdf_channels = 3;
int flag; float3 diffuse_weight = make_float3(0.0f, 0.0f, 0.0f);
/* Verify if the radii are large enough to sample without precision issues. */
if(bssrdf->radius.x < BSSRDF_MIN_RADIUS) {
diffuse_weight.x = bssrdf->weight.x;
bssrdf->weight.x = 0.0f;
bssrdf->radius.x = 0.0f;
bssrdf_channels--;
}
if(bssrdf->radius.y < BSSRDF_MIN_RADIUS) {
diffuse_weight.y = bssrdf->weight.y;
bssrdf->weight.y = 0.0f;
bssrdf->radius.y = 0.0f;
bssrdf_channels--;
}
if(bssrdf->radius.z < BSSRDF_MIN_RADIUS) {
diffuse_weight.z = bssrdf->weight.z;
bssrdf->weight.z = 0.0f;
bssrdf->radius.z = 0.0f;
bssrdf_channels--;
}
if(bssrdf_channels < 3) {
/* Add diffuse BSDF if any radius too small. */
#ifdef __PRINCIPLED__ #ifdef __PRINCIPLED__
if(type == CLOSURE_BSSRDF_PRINCIPLED_ID) { if(type == CLOSURE_BSSRDF_PRINCIPLED_ID) {
float roughness = bssrdf->roughness; float roughness = bssrdf->roughness;
float3 N = bssrdf->N; float3 N = bssrdf->N;
float3 weight = bssrdf->weight;
float sample_weight = bssrdf->sample_weight;
PrincipledDiffuseBsdf *bsdf = (PrincipledDiffuseBsdf*)bssrdf; PrincipledDiffuseBsdf *bsdf = (PrincipledDiffuseBsdf*)bsdf_alloc(sd, sizeof(PrincipledDiffuseBsdf), diffuse_weight);
if(bsdf) {
bsdf->type = CLOSURE_BSDF_BSSRDF_PRINCIPLED_ID;
bsdf->N = N; bsdf->N = N;
bsdf->roughness = roughness; bsdf->roughness = roughness;
bsdf->weight = weight; flag |= bsdf_principled_diffuse_setup(bsdf);
bsdf->sample_weight = sample_weight; }
flag = bsdf_principled_diffuse_setup(bsdf);
bsdf->type = CLOSURE_BSDF_BSSRDF_PRINCIPLED_ID;
} }
else else
#endif /* __PRINCIPLED__ */ #endif /* __PRINCIPLED__ */
{ {
DiffuseBsdf *bsdf = (DiffuseBsdf*)bssrdf; DiffuseBsdf *bsdf = (DiffuseBsdf*)bsdf_alloc(sd, sizeof(DiffuseBsdf), diffuse_weight);
bsdf->N = bssrdf->N;
flag = bsdf_diffuse_setup(bsdf); if(bsdf) {
bsdf->type = CLOSURE_BSDF_BSSRDF_ID; bsdf->type = CLOSURE_BSDF_BSSRDF_ID;
bsdf->N = bssrdf->N;
flag |= bsdf_diffuse_setup(bsdf);
}
}
} }
return flag; /* Setup BSSRDF if radius is large enough. */
} if(bssrdf_channels > 0) {
else { bssrdf->type = type;
bssrdf->channels = bssrdf_channels;
bssrdf->sample_weight = fabsf(average(bssrdf->weight)) * bssrdf->channels;
bssrdf->texture_blur = saturate(bssrdf->texture_blur); bssrdf->texture_blur = saturate(bssrdf->texture_blur);
bssrdf->sharpness = saturate(bssrdf->sharpness); bssrdf->sharpness = saturate(bssrdf->sharpness);
bssrdf->type = type;
if(type == CLOSURE_BSSRDF_BURLEY_ID || if(type == CLOSURE_BSSRDF_BURLEY_ID ||
type == CLOSURE_BSSRDF_PRINCIPLED_ID) type == CLOSURE_BSSRDF_PRINCIPLED_ID)
@@ -386,8 +413,14 @@ ccl_device int bssrdf_setup(Bssrdf *bssrdf, ClosureType type)
bssrdf_burley_setup(bssrdf); bssrdf_burley_setup(bssrdf);
} }
return SD_BSSRDF; flag |= SD_BSSRDF;
} }
else {
bssrdf->type = type;
bssrdf->sample_weight = 0.0f;
}
return flag;
} }
ccl_device void bssrdf_sample(const ShaderClosure *sc, float xi, float *r, float *h) ccl_device void bssrdf_sample(const ShaderClosure *sc, float xi, float *r, float *h)
@@ -395,17 +428,22 @@ ccl_device void bssrdf_sample(const ShaderClosure *sc, float xi, float *r, float
const Bssrdf *bssrdf = (const Bssrdf*)sc; const Bssrdf *bssrdf = (const Bssrdf*)sc;
float radius; float radius;
/* Sample color channel and reuse random number. */ /* Sample color channel and reuse random number. Only a subset of channels
if(xi < 1.0f/3.0f) { * may be used if their radius was too small to handle as BSSRDF. */
xi *= 3.0f; xi *= bssrdf->channels;
radius = bssrdf->radius.x;
if(xi < 1.0f) {
radius = (bssrdf->radius.x > 0.0f)? bssrdf->radius.x:
(bssrdf->radius.y > 0.0f)? bssrdf->radius.y:
bssrdf->radius.z;
} }
else if(xi < 2.0f/3.0f) { else if(xi < 2.0f) {
xi = (xi - 1.0f/3.0f)*3.0f; xi -= 1.0f;
radius = bssrdf->radius.y; radius = (bssrdf->radius.x > 0.0f)? bssrdf->radius.y:
bssrdf->radius.z;
} }
else { else {
xi = (xi - 2.0f/3.0f)*3.0f; xi -= 2.0f;
radius = bssrdf->radius.z; radius = bssrdf->radius.z;
} }
@@ -423,7 +461,10 @@ ccl_device void bssrdf_sample(const ShaderClosure *sc, float xi, float *r, float
ccl_device float bssrdf_channel_pdf(const Bssrdf *bssrdf, float radius, float r) ccl_device float bssrdf_channel_pdf(const Bssrdf *bssrdf, float radius, float r)
{ {
if(bssrdf->type == CLOSURE_BSSRDF_CUBIC_ID) { if(radius == 0.0f) {
return 0.0f;
}
else if(bssrdf->type == CLOSURE_BSSRDF_CUBIC_ID) {
return bssrdf_cubic_pdf(radius, bssrdf->sharpness, r); return bssrdf_cubic_pdf(radius, bssrdf->sharpness, r);
} }
else if(bssrdf->type == CLOSURE_BSSRDF_GAUSSIAN_ID) { else if(bssrdf->type == CLOSURE_BSSRDF_GAUSSIAN_ID) {
@@ -446,7 +487,10 @@ ccl_device_forceinline float3 bssrdf_eval(const ShaderClosure *sc, float r)
ccl_device_forceinline float bssrdf_pdf(const ShaderClosure *sc, float r) ccl_device_forceinline float bssrdf_pdf(const ShaderClosure *sc, float r)
{ {
return average(bssrdf_eval(sc, r)); const Bssrdf *bssrdf = (const Bssrdf*)sc;
float3 pdf = bssrdf_eval(sc, r);
return (pdf.x + pdf.y + pdf.z) / bssrdf->channels;
} }
CCL_NAMESPACE_END CCL_NAMESPACE_END

View File

@@ -51,38 +51,27 @@ using namespace OSL;
class CBSSRDFClosure : public CClosurePrimitive { class CBSSRDFClosure : public CClosurePrimitive {
public: public:
Bssrdf params; Bssrdf params;
float3 radius;
float3 albedo;
void alloc(ShaderData *sd, int path_flag, float3 weight, ClosureType type) void alloc(ShaderData *sd, int path_flag, float3 weight, ClosureType type)
{ {
float sample_weight = fabsf(average(weight)); Bssrdf *bssrdf = bssrdf_alloc(sd, weight);
if(bssrdf) {
/* disable in case of diffuse ancestor, can't see it well then and /* disable in case of diffuse ancestor, can't see it well then and
* adds considerably noise due to probabilities of continuing path * adds considerably noise due to probabilities of continuing path
* getting lower and lower */ * getting lower and lower */
if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR) { if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR) {
radius = make_float3(0.0f, 0.0f, 0.0f); params.radius = make_float3(0.0f, 0.0f, 0.0f);
} }
if(sample_weight > CLOSURE_WEIGHT_CUTOFF) {
/* sharpness */
float sharpness = params.sharpness;
/* texture color blur */
float texture_blur = params.texture_blur;
/* create one closure per color channel */ /* create one closure per color channel */
Bssrdf *bssrdf = bssrdf_alloc(sd, weight); bssrdf->radius = params.radius;
if(bssrdf) { bssrdf->albedo = params.albedo;
bssrdf->sample_weight = sample_weight * 3.0f; bssrdf->texture_blur = params.texture_blur;
bssrdf->radius = radius; bssrdf->sharpness = params.sharpness;
bssrdf->albedo = albedo;
bssrdf->texture_blur = texture_blur;
bssrdf->sharpness = sharpness;
bssrdf->N = params.N; bssrdf->N = params.N;
bssrdf->roughness = params.roughness; bssrdf->roughness = params.roughness;
sd->flag |= bssrdf_setup(bssrdf, (ClosureType)type); sd->flag |= bssrdf_setup(sd, bssrdf, (ClosureType)type);
}
} }
} }
}; };
@@ -101,7 +90,7 @@ ClosureParam *closure_bssrdf_cubic_params()
{ {
static ClosureParam params[] = { static ClosureParam params[] = {
CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, params.N), CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, params.N),
CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, radius), CLOSURE_FLOAT3_PARAM(CubicBSSRDFClosure, params.radius),
CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.texture_blur), CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.texture_blur),
CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.sharpness), CLOSURE_FLOAT_PARAM(CubicBSSRDFClosure, params.sharpness),
CLOSURE_STRING_KEYPARAM(CubicBSSRDFClosure, label, "label"), CLOSURE_STRING_KEYPARAM(CubicBSSRDFClosure, label, "label"),
@@ -126,7 +115,7 @@ ClosureParam *closure_bssrdf_gaussian_params()
{ {
static ClosureParam params[] = { static ClosureParam params[] = {
CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, params.N), CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, params.N),
CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, radius), CLOSURE_FLOAT3_PARAM(GaussianBSSRDFClosure, params.radius),
CLOSURE_FLOAT_PARAM(GaussianBSSRDFClosure, params.texture_blur), CLOSURE_FLOAT_PARAM(GaussianBSSRDFClosure, params.texture_blur),
CLOSURE_STRING_KEYPARAM(GaussianBSSRDFClosure, label, "label"), CLOSURE_STRING_KEYPARAM(GaussianBSSRDFClosure, label, "label"),
CLOSURE_FINISH_PARAM(GaussianBSSRDFClosure) CLOSURE_FINISH_PARAM(GaussianBSSRDFClosure)
@@ -150,9 +139,9 @@ ClosureParam *closure_bssrdf_burley_params()
{ {
static ClosureParam params[] = { static ClosureParam params[] = {
CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, params.N), CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, params.N),
CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, radius), CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, params.radius),
CLOSURE_FLOAT_PARAM(BurleyBSSRDFClosure, params.texture_blur), CLOSURE_FLOAT_PARAM(BurleyBSSRDFClosure, params.texture_blur),
CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, albedo), CLOSURE_FLOAT3_PARAM(BurleyBSSRDFClosure, params.albedo),
CLOSURE_STRING_KEYPARAM(BurleyBSSRDFClosure, label, "label"), CLOSURE_STRING_KEYPARAM(BurleyBSSRDFClosure, label, "label"),
CLOSURE_FINISH_PARAM(BurleyBSSRDFClosure) CLOSURE_FINISH_PARAM(BurleyBSSRDFClosure)
}; };
@@ -175,9 +164,9 @@ ClosureParam *closure_bssrdf_principled_params()
{ {
static ClosureParam params[] = { static ClosureParam params[] = {
CLOSURE_FLOAT3_PARAM(PrincipledBSSRDFClosure, params.N), CLOSURE_FLOAT3_PARAM(PrincipledBSSRDFClosure, params.N),
CLOSURE_FLOAT3_PARAM(PrincipledBSSRDFClosure, radius), CLOSURE_FLOAT3_PARAM(PrincipledBSSRDFClosure, params.radius),
CLOSURE_FLOAT_PARAM(PrincipledBSSRDFClosure, params.texture_blur), CLOSURE_FLOAT_PARAM(PrincipledBSSRDFClosure, params.texture_blur),
CLOSURE_FLOAT3_PARAM(PrincipledBSSRDFClosure, albedo), CLOSURE_FLOAT3_PARAM(PrincipledBSSRDFClosure, params.albedo),
CLOSURE_FLOAT_PARAM(PrincipledBSSRDFClosure, params.roughness), CLOSURE_FLOAT_PARAM(PrincipledBSSRDFClosure, params.roughness),
CLOSURE_STRING_KEYPARAM(PrincipledBSSRDFClosure, label, "label"), CLOSURE_STRING_KEYPARAM(PrincipledBSSRDFClosure, label, "label"),
CLOSURE_FINISH_PARAM(PrincipledBSSRDFClosure) CLOSURE_FINISH_PARAM(PrincipledBSSRDFClosure)

View File

@@ -153,7 +153,6 @@ ccl_device void svm_node_closure_bsdf(KernelGlobals *kg, ShaderData *sd, float *
#ifdef __SUBSURFACE__ #ifdef __SUBSURFACE__
float3 mixed_ss_base_color = subsurface_color * subsurface + base_color * (1.0f - subsurface); float3 mixed_ss_base_color = subsurface_color * subsurface + base_color * (1.0f - subsurface);
float3 subsurf_weight = weight * mixed_ss_base_color * diffuse_weight; float3 subsurf_weight = weight * mixed_ss_base_color * diffuse_weight;
float subsurf_sample_weight = fabsf(average(subsurf_weight));
/* disable in case of diffuse ancestor, can't see it well then and /* disable in case of diffuse ancestor, can't see it well then and
* adds considerably noise due to probabilities of continuing path * adds considerably noise due to probabilities of continuing path
@@ -182,27 +181,19 @@ ccl_device void svm_node_closure_bsdf(KernelGlobals *kg, ShaderData *sd, float *
sd->flag |= bsdf_principled_diffuse_setup(bsdf); sd->flag |= bsdf_principled_diffuse_setup(bsdf);
} }
} }
else if(subsurface > CLOSURE_WEIGHT_CUTOFF && subsurf_sample_weight > CLOSURE_WEIGHT_CUTOFF) { else if(subsurface > CLOSURE_WEIGHT_CUTOFF) {
/* radius * scale */
float3 radius = subsurface_radius * subsurface;
/* sharpness */
float sharpness = 0.0f;
/* texture color blur */
float texture_blur = 0.0f;
/* create one closure per color channel */
Bssrdf *bssrdf = bssrdf_alloc(sd, subsurf_weight); Bssrdf *bssrdf = bssrdf_alloc(sd, subsurf_weight);
if(bssrdf) { if(bssrdf) {
bssrdf->sample_weight = subsurf_sample_weight * 3.0f; bssrdf->radius = subsurface_radius * subsurface;
bssrdf->radius = radius;
bssrdf->albedo = subsurface_color; bssrdf->albedo = subsurface_color;
bssrdf->texture_blur = texture_blur; bssrdf->texture_blur = 0.0f;
bssrdf->sharpness = sharpness; bssrdf->sharpness = 0.0f;
bssrdf->N = N; bssrdf->N = N;
bssrdf->roughness = roughness; bssrdf->roughness = roughness;
/* setup bsdf */ /* setup bsdf */
sd->flag |= bssrdf_setup(bssrdf, (ClosureType)CLOSURE_BSSRDF_PRINCIPLED_ID); sd->flag |= bssrdf_setup(sd, bssrdf, (ClosureType)CLOSURE_BSSRDF_PRINCIPLED_ID);
} }
} }
} }
@@ -756,35 +747,22 @@ ccl_device void svm_node_closure_bsdf(KernelGlobals *kg, ShaderData *sd, float *
case CLOSURE_BSSRDF_CUBIC_ID: case CLOSURE_BSSRDF_CUBIC_ID:
case CLOSURE_BSSRDF_GAUSSIAN_ID: case CLOSURE_BSSRDF_GAUSSIAN_ID:
case CLOSURE_BSSRDF_BURLEY_ID: { case CLOSURE_BSSRDF_BURLEY_ID: {
float3 albedo = sd->svm_closure_weight;
float3 weight = sd->svm_closure_weight * mix_weight; float3 weight = sd->svm_closure_weight * mix_weight;
float sample_weight = fabsf(average(weight)); Bssrdf *bssrdf = bssrdf_alloc(sd, weight);
if(bssrdf) {
/* disable in case of diffuse ancestor, can't see it well then and /* disable in case of diffuse ancestor, can't see it well then and
* adds considerably noise due to probabilities of continuing path * adds considerably noise due to probabilities of continuing path
* getting lower and lower */ * getting lower and lower */
if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR) if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR)
param1 = 0.0f; param1 = 0.0f;
if(sample_weight > CLOSURE_WEIGHT_CUTOFF) { bssrdf->radius = stack_load_float3(stack, data_node.z)*param1;
/* radius * scale */ bssrdf->albedo = sd->svm_closure_weight;
float3 radius = stack_load_float3(stack, data_node.z)*param1; bssrdf->texture_blur = param2;
/* sharpness */ bssrdf->sharpness = stack_load_float(stack, data_node.w);
float sharpness = stack_load_float(stack, data_node.w);
/* texture color blur */
float texture_blur = param2;
/* create one closure per color channel */
Bssrdf *bssrdf = bssrdf_alloc(sd, weight);
if(bssrdf) {
bssrdf->sample_weight = sample_weight * 3.0f;
bssrdf->radius = radius;
bssrdf->albedo = albedo;
bssrdf->texture_blur = texture_blur;
bssrdf->sharpness = sharpness;
bssrdf->N = N; bssrdf->N = N;
sd->flag |= bssrdf_setup(bssrdf, (ClosureType)type); sd->flag |= bssrdf_setup(sd, bssrdf, (ClosureType)type);
}
} }
break; break;