2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/* Closure Nodes */
|
|
|
|
|
2012-11-06 19:59:02 +00:00
|
|
|
__device void svm_node_glass_setup(ShaderData *sd, ShaderClosure *sc, int type, float eta, float roughness, bool refract)
|
2011-09-12 13:13:56 +00:00
|
|
|
{
|
2012-11-06 19:59:02 +00:00
|
|
|
if(type == CLOSURE_BSDF_SHARP_GLASS_ID) {
|
2012-10-20 12:18:00 +00:00
|
|
|
if(refract) {
|
|
|
|
sc->data0 = eta;
|
2013-04-02 15:53:24 +00:00
|
|
|
sc->data1 = 0.0f;
|
2012-10-20 12:18:00 +00:00
|
|
|
sd->flag |= bsdf_refraction_setup(sc);
|
|
|
|
}
|
2011-09-12 13:13:56 +00:00
|
|
|
else
|
2012-10-20 12:18:00 +00:00
|
|
|
sd->flag |= bsdf_reflection_setup(sc);
|
2011-09-12 13:13:56 +00:00
|
|
|
}
|
2012-11-06 19:59:02 +00:00
|
|
|
else if(type == CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID) {
|
2012-10-20 12:18:00 +00:00
|
|
|
sc->data0 = roughness;
|
|
|
|
sc->data1 = eta;
|
|
|
|
|
|
|
|
if(refract)
|
|
|
|
sd->flag |= bsdf_microfacet_beckmann_refraction_setup(sc);
|
|
|
|
else
|
|
|
|
sd->flag |= bsdf_microfacet_beckmann_setup(sc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sc->data0 = roughness;
|
|
|
|
sc->data1 = eta;
|
|
|
|
|
|
|
|
if(refract)
|
|
|
|
sd->flag |= bsdf_microfacet_ggx_refraction_setup(sc);
|
|
|
|
else
|
|
|
|
sd->flag |= bsdf_microfacet_ggx_setup(sc);
|
2011-09-12 13:13:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
__device_inline ShaderClosure *svm_node_closure_get_non_bsdf(ShaderData *sd, ClosureType type, float mix_weight)
|
2011-09-12 13:13:56 +00:00
|
|
|
{
|
|
|
|
#ifdef __MULTI_CLOSURE__
|
|
|
|
ShaderClosure *sc = &sd->closure[sd->num_closure];
|
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
if(sd->num_closure < MAX_CLOSURE) {
|
|
|
|
sc->weight *= mix_weight;
|
|
|
|
sc->type = type;
|
2013-04-02 15:53:24 +00:00
|
|
|
#ifdef __OSL__
|
|
|
|
sc->prim = NULL;
|
|
|
|
#endif
|
2011-09-12 13:13:56 +00:00
|
|
|
sd->num_closure++;
|
2012-11-30 18:55:04 +00:00
|
|
|
return sc;
|
|
|
|
}
|
2011-09-12 13:13:56 +00:00
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
return NULL;
|
2011-09-12 13:13:56 +00:00
|
|
|
#else
|
|
|
|
return &sd->closure;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
__device_inline ShaderClosure *svm_node_closure_get_bsdf(ShaderData *sd, float mix_weight)
|
2011-09-12 13:13:56 +00:00
|
|
|
{
|
|
|
|
#ifdef __MULTI_CLOSURE__
|
2012-11-26 21:59:41 +00:00
|
|
|
ShaderClosure *sc = &sd->closure[sd->num_closure];
|
2012-11-30 18:55:04 +00:00
|
|
|
float3 weight = sc->weight * mix_weight;
|
2012-12-01 22:00:25 +00:00
|
|
|
float sample_weight = fabsf(average(weight));
|
2012-11-26 21:59:41 +00:00
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
if(sample_weight > 1e-5f && sd->num_closure < MAX_CLOSURE) {
|
|
|
|
sc->weight = weight;
|
|
|
|
sc->sample_weight = sample_weight;
|
2012-11-26 21:59:41 +00:00
|
|
|
sd->num_closure++;
|
2013-04-02 15:53:24 +00:00
|
|
|
#ifdef __OSL__
|
|
|
|
sc->prim = NULL;
|
|
|
|
#endif
|
2012-11-26 21:59:41 +00:00
|
|
|
return sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
return &sd->closure;
|
2011-09-12 13:13:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-10-10 15:56:43 +00:00
|
|
|
__device void svm_node_closure_bsdf(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, float randb, int path_flag, int *offset)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-05-13 12:11:08 +00:00
|
|
|
uint type, param1_offset, param2_offset;
|
2011-09-12 13:13:56 +00:00
|
|
|
|
|
|
|
#ifdef __MULTI_CLOSURE__
|
|
|
|
uint mix_weight_offset;
|
|
|
|
decode_node_uchar4(node.y, &type, ¶m1_offset, ¶m2_offset, &mix_weight_offset);
|
|
|
|
float mix_weight = (stack_valid(mix_weight_offset)? stack_load_float(stack, mix_weight_offset): 1.0f);
|
|
|
|
|
2012-10-10 15:56:43 +00:00
|
|
|
/* note we read this extra node before weight check, so offset is added */
|
|
|
|
uint4 data_node = read_node(kg, offset);
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
if(mix_weight == 0.0f)
|
|
|
|
return;
|
2012-10-10 15:56:43 +00:00
|
|
|
|
2013-09-03 22:39:17 +00:00
|
|
|
float3 N = stack_valid(data_node.x)? stack_load_float3(stack, data_node.x): sd->N;
|
2011-09-12 13:13:56 +00:00
|
|
|
#else
|
2011-05-13 12:11:08 +00:00
|
|
|
decode_node_uchar4(node.y, &type, ¶m1_offset, ¶m2_offset, NULL);
|
2011-09-12 13:13:56 +00:00
|
|
|
float mix_weight = 1.0f;
|
2012-10-10 15:56:43 +00:00
|
|
|
|
|
|
|
uint4 data_node = read_node(kg, offset);
|
2013-09-03 22:39:17 +00:00
|
|
|
float3 N = stack_valid(data_node.x)? stack_load_float3(stack, data_node.x): sd->N;
|
2011-09-12 13:13:56 +00:00
|
|
|
#endif
|
2011-05-13 12:11:08 +00:00
|
|
|
|
2013-06-07 16:06:17 +00:00
|
|
|
float param1 = (stack_valid(param1_offset))? stack_load_float(stack, param1_offset): __uint_as_float(node.z);
|
|
|
|
float param2 = (stack_valid(param2_offset))? stack_load_float(stack, param2_offset): __uint_as_float(node.w);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
switch(type) {
|
2011-09-12 13:13:56 +00:00
|
|
|
case CLOSURE_BSDF_DIFFUSE_ID: {
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2011-11-14 17:31:47 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
if(sc) {
|
|
|
|
sc->N = N;
|
2012-10-20 12:18:00 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
float roughness = param1;
|
|
|
|
|
|
|
|
if(roughness == 0.0f) {
|
2013-04-02 15:53:24 +00:00
|
|
|
sc->data0 = 0.0f;
|
|
|
|
sc->data1 = 0.0f;
|
2012-11-26 21:59:41 +00:00
|
|
|
sd->flag |= bsdf_diffuse_setup(sc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sc->data0 = roughness;
|
2013-04-02 15:53:24 +00:00
|
|
|
sc->data1 = 0.0f;
|
2012-11-26 21:59:41 +00:00
|
|
|
sd->flag |= bsdf_oren_nayar_setup(sc);
|
|
|
|
}
|
2012-10-20 12:18:00 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
break;
|
2011-09-12 13:13:56 +00:00
|
|
|
}
|
|
|
|
case CLOSURE_BSDF_TRANSLUCENT_ID: {
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2012-11-26 21:59:41 +00:00
|
|
|
|
|
|
|
if(sc) {
|
2013-04-02 15:53:24 +00:00
|
|
|
sc->data0 = 0.0f;
|
|
|
|
sc->data1 = 0.0f;
|
2012-11-26 21:59:41 +00:00
|
|
|
sc->N = N;
|
|
|
|
sd->flag |= bsdf_translucent_setup(sc);
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
break;
|
2011-09-12 13:13:56 +00:00
|
|
|
}
|
|
|
|
case CLOSURE_BSDF_TRANSPARENT_ID: {
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2012-11-26 21:59:41 +00:00
|
|
|
|
|
|
|
if(sc) {
|
2013-04-02 15:53:24 +00:00
|
|
|
sc->data0 = 0.0f;
|
|
|
|
sc->data1 = 0.0f;
|
2012-11-26 21:59:41 +00:00
|
|
|
sc->N = N;
|
|
|
|
sd->flag |= bsdf_transparent_setup(sc);
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
break;
|
2011-09-12 13:13:56 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
case CLOSURE_BSDF_REFLECTION_ID:
|
|
|
|
case CLOSURE_BSDF_MICROFACET_GGX_ID:
|
|
|
|
case CLOSURE_BSDF_MICROFACET_BECKMANN_ID: {
|
2011-09-12 13:13:56 +00:00
|
|
|
#ifdef __CAUSTICS_TRICKS__
|
|
|
|
if(kernel_data.integrator.no_caustics && (path_flag & PATH_RAY_DIFFUSE))
|
|
|
|
break;
|
|
|
|
#endif
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2012-11-26 21:59:41 +00:00
|
|
|
|
|
|
|
if(sc) {
|
|
|
|
sc->N = N;
|
|
|
|
sc->data0 = param1;
|
2013-04-02 15:53:24 +00:00
|
|
|
sc->data1 = 0.0f;
|
2012-11-26 21:59:41 +00:00
|
|
|
|
|
|
|
/* setup bsdf */
|
|
|
|
if(type == CLOSURE_BSDF_REFLECTION_ID)
|
|
|
|
sd->flag |= bsdf_reflection_setup(sc);
|
|
|
|
else if(type == CLOSURE_BSDF_MICROFACET_BECKMANN_ID)
|
|
|
|
sd->flag |= bsdf_microfacet_beckmann_setup(sc);
|
|
|
|
else
|
|
|
|
sd->flag |= bsdf_microfacet_ggx_setup(sc);
|
|
|
|
}
|
2011-09-16 13:14:02 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CLOSURE_BSDF_REFRACTION_ID:
|
|
|
|
case CLOSURE_BSDF_MICROFACET_GGX_REFRACTION_ID:
|
|
|
|
case CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID: {
|
2011-09-12 13:13:56 +00:00
|
|
|
#ifdef __CAUSTICS_TRICKS__
|
|
|
|
if(kernel_data.integrator.no_caustics && (path_flag & PATH_RAY_DIFFUSE))
|
|
|
|
break;
|
2012-11-06 19:59:02 +00:00
|
|
|
#endif
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2012-11-26 21:59:41 +00:00
|
|
|
|
|
|
|
if(sc) {
|
|
|
|
sc->N = N;
|
|
|
|
sc->data0 = param1;
|
|
|
|
|
|
|
|
float eta = fmaxf(param2, 1.0f + 1e-5f);
|
|
|
|
sc->data1 = (sd->flag & SD_BACKFACING)? 1.0f/eta: eta;
|
|
|
|
|
|
|
|
/* setup bsdf */
|
|
|
|
if(type == CLOSURE_BSDF_REFRACTION_ID)
|
|
|
|
sd->flag |= bsdf_refraction_setup(sc);
|
|
|
|
else if(type == CLOSURE_BSDF_MICROFACET_BECKMANN_REFRACTION_ID)
|
|
|
|
sd->flag |= bsdf_microfacet_beckmann_refraction_setup(sc);
|
|
|
|
else
|
|
|
|
sd->flag |= bsdf_microfacet_ggx_refraction_setup(sc);
|
|
|
|
}
|
2012-11-06 19:59:02 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CLOSURE_BSDF_SHARP_GLASS_ID:
|
|
|
|
case CLOSURE_BSDF_MICROFACET_GGX_GLASS_ID:
|
|
|
|
case CLOSURE_BSDF_MICROFACET_BECKMANN_GLASS_ID: {
|
|
|
|
#ifdef __CAUSTICS_TRICKS__
|
|
|
|
if(kernel_data.integrator.no_caustics && (path_flag & PATH_RAY_DIFFUSE))
|
|
|
|
break;
|
2011-09-12 13:13:56 +00:00
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
/* index of refraction */
|
2011-09-16 13:14:02 +00:00
|
|
|
float eta = fmaxf(param2, 1.0f + 1e-5f);
|
|
|
|
eta = (sd->flag & SD_BACKFACING)? 1.0f/eta: eta;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* fresnel */
|
2012-10-10 15:56:43 +00:00
|
|
|
float cosNO = dot(N, sd->I);
|
2011-04-27 11:58:34 +00:00
|
|
|
float fresnel = fresnel_dielectric_cos(cosNO, eta);
|
2011-09-12 13:13:56 +00:00
|
|
|
float roughness = param1;
|
|
|
|
|
|
|
|
#ifdef __MULTI_CLOSURE__
|
|
|
|
/* reflection */
|
2012-11-26 21:59:41 +00:00
|
|
|
ShaderClosure *sc = &sd->closure[sd->num_closure];
|
2011-09-12 13:13:56 +00:00
|
|
|
float3 weight = sc->weight;
|
|
|
|
float sample_weight = sc->sample_weight;
|
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
sc = svm_node_closure_get_bsdf(sd, mix_weight*fresnel);
|
2011-09-12 13:13:56 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
if(sc) {
|
|
|
|
sc->N = N;
|
|
|
|
svm_node_glass_setup(sd, sc, type, eta, roughness, false);
|
|
|
|
}
|
2011-09-12 13:13:56 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
/* refraction */
|
|
|
|
sc = &sd->closure[sd->num_closure];
|
2011-09-12 13:13:56 +00:00
|
|
|
sc->weight = weight;
|
|
|
|
sc->sample_weight = sample_weight;
|
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
sc = svm_node_closure_get_bsdf(sd, mix_weight*(1.0f - fresnel));
|
2011-09-12 13:13:56 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
if(sc) {
|
|
|
|
sc->N = N;
|
|
|
|
svm_node_glass_setup(sd, sc, type, eta, roughness, true);
|
|
|
|
}
|
|
|
|
#else
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2011-09-12 13:13:56 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
if(sc) {
|
|
|
|
sc->N = N;
|
|
|
|
bool refract = (randb > fresnel);
|
|
|
|
svm_node_glass_setup(sd, sc, type, eta, roughness, refract);
|
|
|
|
}
|
2011-09-12 13:13:56 +00:00
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CLOSURE_BSDF_WARD_ID: {
|
2011-09-12 13:13:56 +00:00
|
|
|
#ifdef __CAUSTICS_TRICKS__
|
|
|
|
if(kernel_data.integrator.no_caustics && (path_flag & PATH_RAY_DIFFUSE))
|
|
|
|
break;
|
|
|
|
#endif
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2012-11-26 21:59:41 +00:00
|
|
|
|
|
|
|
if(sc) {
|
|
|
|
sc->N = N;
|
2011-09-12 13:13:56 +00:00
|
|
|
|
2012-11-22 16:08:18 +00:00
|
|
|
#ifdef __ANISOTROPIC__
|
2013-09-03 22:39:17 +00:00
|
|
|
sc->T = stack_load_float3(stack, data_node.y);
|
2012-11-22 16:08:18 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
/* rotate tangent */
|
2013-09-03 22:39:17 +00:00
|
|
|
float rotation = stack_load_float(stack, data_node.z);
|
2012-11-04 22:31:32 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
if(rotation != 0.0f)
|
2013-05-12 14:13:29 +00:00
|
|
|
sc->T = rotate_around_axis(sc->T, sc->N, rotation * M_2PI_F);
|
2012-11-04 22:31:32 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
/* compute roughness */
|
|
|
|
float roughness = param1;
|
|
|
|
float anisotropy = clamp(param2, -0.99f, 0.99f);
|
2012-11-04 22:31:32 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
if(anisotropy < 0.0f) {
|
|
|
|
sc->data0 = roughness/(1.0f + anisotropy);
|
|
|
|
sc->data1 = roughness*(1.0f + anisotropy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sc->data0 = roughness*(1.0f - anisotropy);
|
|
|
|
sc->data1 = roughness/(1.0f - anisotropy);
|
|
|
|
}
|
2011-09-12 13:13:56 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
sd->flag |= bsdf_ward_setup(sc);
|
2012-11-22 16:08:18 +00:00
|
|
|
#else
|
2012-11-26 21:59:41 +00:00
|
|
|
sd->flag |= bsdf_diffuse_setup(sc);
|
2012-11-22 16:08:18 +00:00
|
|
|
#endif
|
2012-11-26 21:59:41 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CLOSURE_BSDF_ASHIKHMIN_VELVET_ID: {
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2011-09-12 13:13:56 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
if(sc) {
|
|
|
|
sc->N = N;
|
|
|
|
|
|
|
|
/* sigma */
|
|
|
|
sc->data0 = clamp(param1, 0.0f, 1.0f);
|
2013-04-02 15:53:24 +00:00
|
|
|
sc->data1 = 0.0f;
|
2012-11-26 21:59:41 +00:00
|
|
|
sd->flag |= bsdf_ashikhmin_velvet_setup(sc);
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-05-23 17:45:20 +00:00
|
|
|
case CLOSURE_BSDF_DIFFUSE_TOON_ID:
|
|
|
|
case CLOSURE_BSDF_GLOSSY_TOON_ID: {
|
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
|
|
|
|
|
|
|
if(sc) {
|
|
|
|
/* Normal, Size and Smooth */
|
|
|
|
sc->N = N;
|
|
|
|
sc->data0 = param1;
|
|
|
|
sc->data1 = param2;
|
|
|
|
|
|
|
|
if (type == CLOSURE_BSDF_DIFFUSE_TOON_ID)
|
|
|
|
sd->flag |= bsdf_diffuse_toon_setup(sc);
|
|
|
|
else
|
|
|
|
sd->flag |= bsdf_glossy_toon_setup(sc);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-09-15 23:58:00 +00:00
|
|
|
#ifdef __HAIR__
|
|
|
|
case CLOSURE_BSDF_HAIR_REFLECTION_ID:
|
|
|
|
case CLOSURE_BSDF_HAIR_TRANSMISSION_ID: {
|
|
|
|
|
|
|
|
if(sd->flag & SD_BACKFACING && sd->segment != ~0) {
|
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
|
|
|
if(sc) {
|
|
|
|
sc->weight = make_float3(1.0f,1.0f,1.0f);
|
|
|
|
sc->N = N;
|
|
|
|
sd->flag |= bsdf_transparent_setup(sc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ShaderClosure *sc = &sd->closure[sd->num_closure];
|
|
|
|
sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
|
|
|
|
|
|
|
if(sc) {
|
|
|
|
sc->N = N;
|
|
|
|
sc->data0 = param1;
|
|
|
|
sc->data1 = param2;
|
|
|
|
sc->offset = -stack_load_float(stack, data_node.z);
|
|
|
|
if(sd->segment == ~0) {
|
|
|
|
sc->T = normalize(sd->dPdv);
|
|
|
|
sc->offset = 0.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sc->T = sd->dPdu;
|
|
|
|
if(type == CLOSURE_BSDF_HAIR_REFLECTION_ID) {
|
|
|
|
sd->flag |= bsdf_hair_reflection_setup(sc);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sd->flag |= bsdf_hair_transmission_setup(sc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-01 20:26:52 +00:00
|
|
|
#ifdef __SUBSURFACE__
|
2013-08-18 14:15:57 +00:00
|
|
|
case CLOSURE_BSSRDF_CUBIC_ID:
|
|
|
|
case CLOSURE_BSSRDF_GAUSSIAN_ID: {
|
2013-04-01 20:26:52 +00:00
|
|
|
ShaderClosure *sc = &sd->closure[sd->num_closure];
|
|
|
|
float3 weight = sc->weight * mix_weight;
|
|
|
|
float sample_weight = fabsf(average(weight));
|
2013-08-18 14:15:57 +00:00
|
|
|
|
|
|
|
/* disable in case of diffuse ancestor, can't see it well then and
|
|
|
|
* adds considerably noise due to probabilities of continuing path
|
|
|
|
* getting lower and lower */
|
2013-10-08 17:07:18 +00:00
|
|
|
if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR)
|
2013-08-18 14:15:57 +00:00
|
|
|
param1 = 0.0f;
|
2013-04-01 20:26:52 +00:00
|
|
|
|
|
|
|
if(sample_weight > 1e-5f && sd->num_closure+2 < MAX_CLOSURE) {
|
|
|
|
/* radius * scale */
|
2013-09-03 22:39:17 +00:00
|
|
|
float3 radius = stack_load_float3(stack, data_node.z)*param1;
|
|
|
|
/* sharpness */
|
|
|
|
float sharpness = stack_load_float(stack, data_node.w);
|
2013-08-18 14:15:57 +00:00
|
|
|
/* texture color blur */
|
|
|
|
float texture_blur = param2;
|
2013-04-01 20:26:52 +00:00
|
|
|
|
|
|
|
/* create one closure per color channel */
|
|
|
|
if(fabsf(weight.x) > 0.0f) {
|
|
|
|
sc->weight = make_float3(weight.x, 0.0f, 0.0f);
|
|
|
|
sc->sample_weight = sample_weight;
|
|
|
|
sc->data0 = radius.x;
|
2013-08-18 14:15:57 +00:00
|
|
|
sc->data1 = texture_blur;
|
2013-09-03 22:39:17 +00:00
|
|
|
sc->T.x = sharpness;
|
2013-04-02 15:53:24 +00:00
|
|
|
#ifdef __OSL__
|
|
|
|
sc->prim = NULL;
|
|
|
|
#endif
|
2013-04-01 20:26:52 +00:00
|
|
|
sc->N = N;
|
2013-08-18 14:15:57 +00:00
|
|
|
sd->flag |= bssrdf_setup(sc, (ClosureType)type);
|
2013-04-01 20:26:52 +00:00
|
|
|
|
|
|
|
sd->num_closure++;
|
|
|
|
sc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fabsf(weight.y) > 0.0f) {
|
|
|
|
sc->weight = make_float3(0.0f, weight.y, 0.0f);
|
|
|
|
sc->sample_weight = sample_weight;
|
|
|
|
sc->data0 = radius.y;
|
2013-08-18 14:15:57 +00:00
|
|
|
sc->data1 = texture_blur;
|
2013-09-03 22:39:17 +00:00
|
|
|
sc->T.x = sharpness;
|
2013-04-02 15:53:24 +00:00
|
|
|
#ifdef __OSL__
|
|
|
|
sc->prim = NULL;
|
|
|
|
#endif
|
2013-04-01 20:26:52 +00:00
|
|
|
sc->N = N;
|
2013-08-18 14:15:57 +00:00
|
|
|
sd->flag |= bssrdf_setup(sc, (ClosureType)type);
|
2013-04-01 20:26:52 +00:00
|
|
|
|
|
|
|
sd->num_closure++;
|
|
|
|
sc++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fabsf(weight.z) > 0.0f) {
|
|
|
|
sc->weight = make_float3(0.0f, 0.0f, weight.z);
|
|
|
|
sc->sample_weight = sample_weight;
|
|
|
|
sc->data0 = radius.z;
|
2013-08-18 14:15:57 +00:00
|
|
|
sc->data1 = texture_blur;
|
2013-09-03 22:39:17 +00:00
|
|
|
sc->T.x = sharpness;
|
2013-04-02 15:53:24 +00:00
|
|
|
#ifdef __OSL__
|
|
|
|
sc->prim = NULL;
|
|
|
|
#endif
|
2013-04-01 20:26:52 +00:00
|
|
|
sc->N = N;
|
2013-08-18 14:15:57 +00:00
|
|
|
sd->flag |= bssrdf_setup(sc, (ClosureType)type);
|
2013-04-01 20:26:52 +00:00
|
|
|
|
|
|
|
sd->num_closure++;
|
|
|
|
sc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
default:
|
2011-09-12 13:13:56 +00:00
|
|
|
break;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-27 20:03:16 +00:00
|
|
|
__device void svm_node_closure_volume(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int path_flag)
|
|
|
|
{
|
|
|
|
uint type, param1_offset, param2_offset;
|
|
|
|
|
|
|
|
#ifdef __MULTI_CLOSURE__
|
|
|
|
uint mix_weight_offset;
|
|
|
|
decode_node_uchar4(node.y, &type, ¶m1_offset, ¶m2_offset, &mix_weight_offset);
|
|
|
|
float mix_weight = (stack_valid(mix_weight_offset)? stack_load_float(stack, mix_weight_offset): 1.0f);
|
|
|
|
|
|
|
|
if(mix_weight == 0.0f)
|
|
|
|
return;
|
|
|
|
#else
|
|
|
|
decode_node_uchar4(node.y, &type, ¶m1_offset, ¶m2_offset, NULL);
|
|
|
|
float mix_weight = 1.0f;
|
|
|
|
#endif
|
|
|
|
|
2013-06-07 16:06:17 +00:00
|
|
|
float param1 = (stack_valid(param1_offset))? stack_load_float(stack, param1_offset): __uint_as_float(node.z);
|
|
|
|
//float param2 = (stack_valid(param2_offset))? stack_load_float(stack, param2_offset): __uint_as_float(node.w);
|
2011-09-27 20:03:16 +00:00
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case CLOSURE_VOLUME_TRANSPARENT_ID: {
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2011-09-27 20:03:16 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
if(sc) {
|
|
|
|
float density = param1;
|
|
|
|
sd->flag |= volume_transparent_setup(sc, density);
|
|
|
|
}
|
2011-09-27 20:03:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CLOSURE_VOLUME_ISOTROPIC_ID: {
|
2012-11-30 18:55:04 +00:00
|
|
|
ShaderClosure *sc = svm_node_closure_get_bsdf(sd, mix_weight);
|
2011-09-27 20:03:16 +00:00
|
|
|
|
2012-11-26 21:59:41 +00:00
|
|
|
if(sc) {
|
|
|
|
float density = param1;
|
|
|
|
sd->flag |= volume_isotropic_setup(sc, density);
|
|
|
|
}
|
2011-09-27 20:03:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
__device void svm_node_closure_emission(ShaderData *sd, float *stack, uint4 node)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-09-12 13:13:56 +00:00
|
|
|
#ifdef __MULTI_CLOSURE__
|
|
|
|
uint mix_weight_offset = node.y;
|
|
|
|
|
|
|
|
if(stack_valid(mix_weight_offset)) {
|
|
|
|
float mix_weight = stack_load_float(stack, mix_weight_offset);
|
|
|
|
|
|
|
|
if(mix_weight == 0.0f)
|
|
|
|
return;
|
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
svm_node_closure_get_non_bsdf(sd, CLOSURE_EMISSION_ID, mix_weight);
|
2011-10-19 00:13:41 +00:00
|
|
|
}
|
2012-11-30 18:55:04 +00:00
|
|
|
else
|
|
|
|
svm_node_closure_get_non_bsdf(sd, CLOSURE_EMISSION_ID, 1.0f);
|
2011-09-12 13:13:56 +00:00
|
|
|
#else
|
|
|
|
ShaderClosure *sc = &sd->closure;
|
2011-10-19 00:13:41 +00:00
|
|
|
sc->type = CLOSURE_EMISSION_ID;
|
2011-09-12 13:13:56 +00:00
|
|
|
#endif
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
sd->flag |= SD_EMISSION;
|
|
|
|
}
|
|
|
|
|
2011-10-19 00:13:41 +00:00
|
|
|
__device void svm_node_closure_background(ShaderData *sd, float *stack, uint4 node)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-09-12 13:13:56 +00:00
|
|
|
#ifdef __MULTI_CLOSURE__
|
2011-10-19 00:13:41 +00:00
|
|
|
uint mix_weight_offset = node.y;
|
|
|
|
|
|
|
|
if(stack_valid(mix_weight_offset)) {
|
|
|
|
float mix_weight = stack_load_float(stack, mix_weight_offset);
|
|
|
|
|
|
|
|
if(mix_weight == 0.0f)
|
|
|
|
return;
|
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
svm_node_closure_get_non_bsdf(sd, CLOSURE_BACKGROUND_ID, mix_weight);
|
2011-10-19 00:13:41 +00:00
|
|
|
}
|
2012-11-30 18:55:04 +00:00
|
|
|
else
|
|
|
|
svm_node_closure_get_non_bsdf(sd, CLOSURE_BACKGROUND_ID, 1.0f);
|
2011-09-12 13:13:56 +00:00
|
|
|
#else
|
|
|
|
ShaderClosure *sc = &sd->closure;
|
|
|
|
sc->type = CLOSURE_BACKGROUND_ID;
|
2011-10-19 00:13:41 +00:00
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
__device void svm_node_closure_holdout(ShaderData *sd, float *stack, uint4 node)
|
2011-08-28 13:55:59 +00:00
|
|
|
{
|
2011-09-12 13:13:56 +00:00
|
|
|
#ifdef __MULTI_CLOSURE__
|
|
|
|
uint mix_weight_offset = node.y;
|
|
|
|
|
|
|
|
if(stack_valid(mix_weight_offset)) {
|
|
|
|
float mix_weight = stack_load_float(stack, mix_weight_offset);
|
|
|
|
|
|
|
|
if(mix_weight == 0.0f)
|
|
|
|
return;
|
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
svm_node_closure_get_non_bsdf(sd, CLOSURE_HOLDOUT_ID, mix_weight);
|
2011-10-19 00:13:41 +00:00
|
|
|
}
|
2012-11-30 18:55:04 +00:00
|
|
|
else
|
|
|
|
svm_node_closure_get_non_bsdf(sd, CLOSURE_HOLDOUT_ID, 1.0f);
|
2011-09-12 13:13:56 +00:00
|
|
|
#else
|
|
|
|
ShaderClosure *sc = &sd->closure;
|
2011-10-19 00:13:41 +00:00
|
|
|
sc->type = CLOSURE_HOLDOUT_ID;
|
2011-09-12 13:13:56 +00:00
|
|
|
#endif
|
|
|
|
|
2011-08-28 13:55:59 +00:00
|
|
|
sd->flag |= SD_HOLDOUT;
|
|
|
|
}
|
|
|
|
|
2012-11-06 19:59:02 +00:00
|
|
|
__device void svm_node_closure_ambient_occlusion(ShaderData *sd, float *stack, uint4 node)
|
|
|
|
{
|
|
|
|
#ifdef __MULTI_CLOSURE__
|
|
|
|
uint mix_weight_offset = node.y;
|
|
|
|
|
|
|
|
if(stack_valid(mix_weight_offset)) {
|
|
|
|
float mix_weight = stack_load_float(stack, mix_weight_offset);
|
|
|
|
|
|
|
|
if(mix_weight == 0.0f)
|
|
|
|
return;
|
|
|
|
|
2012-11-30 18:55:04 +00:00
|
|
|
svm_node_closure_get_non_bsdf(sd, CLOSURE_AMBIENT_OCCLUSION_ID, mix_weight);
|
2012-11-06 19:59:02 +00:00
|
|
|
}
|
2012-11-30 18:55:04 +00:00
|
|
|
else
|
|
|
|
svm_node_closure_get_non_bsdf(sd, CLOSURE_AMBIENT_OCCLUSION_ID, 1.0f);
|
2012-11-06 19:59:02 +00:00
|
|
|
#else
|
|
|
|
ShaderClosure *sc = &sd->closure;
|
|
|
|
sc->type = CLOSURE_AMBIENT_OCCLUSION_ID;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
sd->flag |= SD_AO;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Closure Nodes */
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
__device_inline void svm_node_closure_store_weight(ShaderData *sd, float3 weight)
|
|
|
|
{
|
|
|
|
#ifdef __MULTI_CLOSURE__
|
2012-11-30 18:55:04 +00:00
|
|
|
if(sd->num_closure < MAX_CLOSURE)
|
|
|
|
sd->closure[sd->num_closure].weight = weight;
|
2011-09-12 13:13:56 +00:00
|
|
|
#else
|
|
|
|
sd->closure.weight = weight;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
__device void svm_node_closure_set_weight(ShaderData *sd, uint r, uint g, uint b)
|
|
|
|
{
|
2013-06-07 16:06:17 +00:00
|
|
|
float3 weight = make_float3(__uint_as_float(r), __uint_as_float(g), __uint_as_float(b));
|
2011-09-12 13:13:56 +00:00
|
|
|
svm_node_closure_store_weight(sd, weight);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__device void svm_node_emission_set_weight_total(KernelGlobals *kg, ShaderData *sd, uint r, uint g, uint b)
|
|
|
|
{
|
2013-06-07 16:06:17 +00:00
|
|
|
float3 weight = make_float3(__uint_as_float(r), __uint_as_float(g), __uint_as_float(b));
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
if(sd->object != ~0)
|
2011-09-12 13:13:56 +00:00
|
|
|
weight /= object_surface_area(kg, sd->object);
|
|
|
|
|
|
|
|
svm_node_closure_store_weight(sd, weight);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__device void svm_node_closure_weight(ShaderData *sd, float *stack, uint weight_offset)
|
|
|
|
{
|
2011-09-12 13:13:56 +00:00
|
|
|
float3 weight = stack_load_float3(stack, weight_offset);
|
|
|
|
|
|
|
|
svm_node_closure_store_weight(sd, weight);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__device void svm_node_emission_weight(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node)
|
|
|
|
{
|
|
|
|
uint color_offset = node.y;
|
|
|
|
uint strength_offset = node.z;
|
|
|
|
uint total_power = node.w;
|
|
|
|
|
2011-09-16 13:14:02 +00:00
|
|
|
float strength = stack_load_float(stack, strength_offset);
|
|
|
|
float3 weight = stack_load_float3(stack, color_offset)*strength;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
if(total_power && sd->object != ~0)
|
2011-09-12 13:13:56 +00:00
|
|
|
weight /= object_surface_area(kg, sd->object);
|
|
|
|
|
|
|
|
svm_node_closure_store_weight(sd, weight);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__device void svm_node_mix_closure(ShaderData *sd, float *stack,
|
2011-09-12 13:13:56 +00:00
|
|
|
uint4 node, int *offset, float *randb)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-09-12 13:13:56 +00:00
|
|
|
#ifdef __MULTI_CLOSURE__
|
|
|
|
/* fetch weight from blend input, previous mix closures,
|
2012-06-09 17:22:52 +00:00
|
|
|
* and write to stack to be used by closure nodes later */
|
2011-09-12 13:13:56 +00:00
|
|
|
uint weight_offset, in_weight_offset, weight1_offset, weight2_offset;
|
|
|
|
decode_node_uchar4(node.y, &weight_offset, &in_weight_offset, &weight1_offset, &weight2_offset);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
float weight = stack_load_float(stack, weight_offset);
|
2013-06-01 12:23:49 +00:00
|
|
|
weight = clamp(weight, 0.0f, 1.0f);
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
float in_weight = (stack_valid(in_weight_offset))? stack_load_float(stack, in_weight_offset): 1.0f;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
if(stack_valid(weight1_offset))
|
|
|
|
stack_store_float(stack, weight1_offset, in_weight*(1.0f - weight));
|
|
|
|
if(stack_valid(weight2_offset))
|
|
|
|
stack_store_float(stack, weight2_offset, in_weight*weight);
|
|
|
|
#else
|
2011-04-27 11:58:34 +00:00
|
|
|
/* pick a closure and make the random number uniform over 0..1 again.
|
2012-06-09 17:22:52 +00:00
|
|
|
* closure 1 starts on the next node, for closure 2 the start is at an
|
|
|
|
* offset from the current node, so we jump */
|
2011-09-12 13:13:56 +00:00
|
|
|
uint weight_offset = node.y;
|
|
|
|
uint node_jump = node.z;
|
|
|
|
float weight = stack_load_float(stack, weight_offset);
|
|
|
|
weight = clamp(weight, 0.0f, 1.0f);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
if(*randb < weight) {
|
|
|
|
*offset += node_jump;
|
2011-05-14 13:23:15 +00:00
|
|
|
*randb = *randb/weight;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else
|
2011-05-14 13:23:15 +00:00
|
|
|
*randb = (*randb - weight)/(1.0f - weight);
|
2011-09-12 13:13:56 +00:00
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
__device void svm_node_add_closure(ShaderData *sd, float *stack, uint unused,
|
|
|
|
uint node_jump, int *offset, float *randb, float *closure_weight)
|
|
|
|
{
|
2011-09-12 13:13:56 +00:00
|
|
|
#ifdef __MULTI_CLOSURE__
|
|
|
|
/* nothing to do, handled in compiler */
|
|
|
|
#else
|
2011-04-27 11:58:34 +00:00
|
|
|
/* pick one of the two closures with probability 0.5. sampling quality
|
2012-06-09 17:22:52 +00:00
|
|
|
* is not going to be great, for that we'd need to evaluate the weights
|
|
|
|
* of the two closures being added */
|
2011-09-12 13:13:56 +00:00
|
|
|
float weight = 0.5f;
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
if(*randb < weight) {
|
|
|
|
*offset += node_jump;
|
2011-05-14 13:23:15 +00:00
|
|
|
*randb = *randb/weight;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
else
|
2011-05-14 13:23:15 +00:00
|
|
|
*randb = (*randb - weight)/(1.0f - weight);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
*closure_weight *= 2.0f;
|
2011-09-12 13:13:56 +00:00
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2012-10-10 15:56:43 +00:00
|
|
|
/* (Bump) normal */
|
|
|
|
|
|
|
|
__device void svm_node_set_normal(KernelGlobals *kg, ShaderData *sd, float *stack, uint in_direction, uint out_normal)
|
|
|
|
{
|
|
|
|
float3 normal = stack_load_float3(stack, in_direction);
|
|
|
|
sd->N = normal;
|
|
|
|
stack_store_float3(stack, out_normal, normal);
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|