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
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-06 07:39:19 +00:00
|
|
|
#include <OSL/oslexec.h>
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "kernel_compat_cpu.h"
|
2013-08-18 14:15:57 +00:00
|
|
|
#include "kernel_montecarlo.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "kernel_types.h"
|
|
|
|
#include "kernel_globals.h"
|
2014-03-29 12:03:45 +00:00
|
|
|
|
|
|
|
#include "geom/geom_object.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-08-18 14:15:57 +00:00
|
|
|
#include "closure/bsdf_diffuse.h"
|
|
|
|
#include "closure/bssrdf.h"
|
|
|
|
|
2013-04-01 20:26:52 +00:00
|
|
|
#include "osl_bssrdf.h"
|
2012-10-20 12:18:00 +00:00
|
|
|
#include "osl_closures.h"
|
2012-12-01 19:15:05 +00:00
|
|
|
#include "osl_globals.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "osl_services.h"
|
|
|
|
#include "osl_shader.h"
|
|
|
|
|
|
|
|
#include "util_foreach.h"
|
|
|
|
|
2013-01-03 12:08:54 +00:00
|
|
|
#include "attribute.h"
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/* Threads */
|
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
void OSLShader::thread_init(KernelGlobals *kg, KernelGlobals *kernel_globals, OSLGlobals *osl_globals)
|
2012-11-09 03:10:22 +00:00
|
|
|
{
|
2012-12-01 19:15:05 +00:00
|
|
|
/* no osl used? */
|
|
|
|
if(!osl_globals->use) {
|
|
|
|
kg->osl = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2012-11-09 03:10:22 +00:00
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
/* per thread kernel data init*/
|
|
|
|
kg->osl = osl_globals;
|
2013-06-28 13:05:21 +00:00
|
|
|
kg->osl->services->thread_init(kernel_globals, osl_globals->ts);
|
2012-11-09 03:10:22 +00:00
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
OSL::ShadingSystem *ss = kg->osl->ss;
|
|
|
|
OSLThreadData *tdata = new OSLThreadData();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
memset(&tdata->globals, 0, sizeof(OSL::ShaderGlobals));
|
2012-12-15 10:18:42 +00:00
|
|
|
tdata->globals.tracedata = &tdata->tracedata;
|
|
|
|
tdata->globals.flipHandedness = false;
|
2013-08-05 12:49:15 +00:00
|
|
|
tdata->osl_thread_info = ss->create_thread_info();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-12-15 10:18:42 +00:00
|
|
|
for(int i = 0; i < SHADER_CONTEXT_NUM; i++)
|
2013-08-05 12:49:15 +00:00
|
|
|
tdata->context[i] = ss->get_context(tdata->osl_thread_info);
|
|
|
|
|
|
|
|
tdata->oiio_thread_info = osl_globals->ts->get_perthread_info();
|
2012-12-15 10:18:42 +00:00
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
kg->osl_ss = (OSLShadingSystem*)ss;
|
|
|
|
kg->osl_tdata = tdata;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OSLShader::thread_free(KernelGlobals *kg)
|
|
|
|
{
|
2012-12-01 19:15:05 +00:00
|
|
|
if(!kg->osl)
|
|
|
|
return;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
|
|
|
|
OSLThreadData *tdata = kg->osl_tdata;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-12-15 10:18:42 +00:00
|
|
|
for(int i = 0; i < SHADER_CONTEXT_NUM; i++)
|
|
|
|
ss->release_context(tdata->context[i]);
|
|
|
|
|
2013-08-05 12:49:15 +00:00
|
|
|
ss->destroy_thread_info(tdata->osl_thread_info);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
delete tdata;
|
2012-12-01 19:15:05 +00:00
|
|
|
|
|
|
|
kg->osl = NULL;
|
|
|
|
kg->osl_ss = NULL;
|
|
|
|
kg->osl_tdata = NULL;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Globals */
|
|
|
|
|
2016-01-06 22:38:13 +00:00
|
|
|
static void shaderdata_to_shaderglobals(KernelGlobals *kg, ShaderData *sd, PathState *state,
|
2012-12-15 10:18:42 +00:00
|
|
|
int path_flag, OSLThreadData *tdata)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-12-15 10:18:42 +00:00
|
|
|
OSL::ShaderGlobals *globals = &tdata->globals;
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* copy from shader data to shader globals */
|
|
|
|
globals->P = TO_VEC3(sd->P);
|
|
|
|
globals->dPdx = TO_VEC3(sd->dP.dx);
|
|
|
|
globals->dPdy = TO_VEC3(sd->dP.dy);
|
|
|
|
globals->I = TO_VEC3(sd->I);
|
|
|
|
globals->dIdx = TO_VEC3(sd->dI.dx);
|
|
|
|
globals->dIdy = TO_VEC3(sd->dI.dy);
|
|
|
|
globals->N = TO_VEC3(sd->N);
|
|
|
|
globals->Ng = TO_VEC3(sd->Ng);
|
|
|
|
globals->u = sd->u;
|
|
|
|
globals->dudx = sd->du.dx;
|
|
|
|
globals->dudy = sd->du.dy;
|
|
|
|
globals->v = sd->v;
|
|
|
|
globals->dvdx = sd->dv.dx;
|
|
|
|
globals->dvdy = sd->dv.dy;
|
|
|
|
globals->dPdu = TO_VEC3(sd->dPdu);
|
|
|
|
globals->dPdv = TO_VEC3(sd->dPdv);
|
2014-03-29 12:03:47 +00:00
|
|
|
globals->surfacearea = (sd->object == OBJECT_NONE) ? 1.0f : object_surface_area(kg, sd->object);
|
2012-11-29 16:11:37 +00:00
|
|
|
globals->time = sd->time;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* booleans */
|
2012-12-15 10:18:42 +00:00
|
|
|
globals->raytype = path_flag;
|
2011-04-27 11:58:34 +00:00
|
|
|
globals->backfacing = (sd->flag & SD_BACKFACING);
|
|
|
|
|
|
|
|
/* shader data to be used in services callbacks */
|
|
|
|
globals->renderstate = sd;
|
|
|
|
|
|
|
|
/* hacky, we leave it to services to fetch actual object matrix */
|
|
|
|
globals->shader2common = sd;
|
|
|
|
globals->object2common = sd;
|
|
|
|
|
|
|
|
/* must be set to NULL before execute */
|
|
|
|
globals->Ci = NULL;
|
2012-12-15 10:18:42 +00:00
|
|
|
|
|
|
|
/* clear trace data */
|
|
|
|
tdata->tracedata.init = false;
|
2013-04-22 14:27:12 +00:00
|
|
|
|
|
|
|
/* used by renderservices */
|
|
|
|
sd->osl_globals = kg;
|
2016-01-06 22:38:13 +00:00
|
|
|
sd->osl_path_state = state;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Surface */
|
|
|
|
|
2013-08-18 14:15:57 +00:00
|
|
|
static void flatten_surface_closure_tree(ShaderData *sd, int path_flag,
|
2012-06-04 22:44:58 +00:00
|
|
|
const OSL::ClosureColor *closure, float3 weight = make_float3(1.0f, 1.0f, 1.0f))
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-06-05 09:29:47 +00:00
|
|
|
/* OSL gives us a closure tree, we flatten it into arrays per
|
2011-04-27 11:58:34 +00:00
|
|
|
* closure type, for evaluation, sampling, etc later on. */
|
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
switch(closure->id) {
|
|
|
|
case OSL::ClosureColor::MUL: {
|
|
|
|
OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
|
|
|
|
flatten_surface_closure_tree(sd, path_flag, mul->closure, TO_FLOAT3(mul->weight) * weight);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OSL::ClosureColor::ADD: {
|
|
|
|
OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
|
|
|
|
flatten_surface_closure_tree(sd, path_flag, add->closureA, weight);
|
|
|
|
flatten_surface_closure_tree(sd, path_flag, add->closureB, weight);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
|
|
|
|
CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
if(prim) {
|
|
|
|
ShaderClosure sc;
|
2013-12-18 11:29:11 +00:00
|
|
|
|
2013-09-09 21:52:26 +00:00
|
|
|
#ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
|
2015-09-23 07:23:37 +00:00
|
|
|
weight = weight*TO_FLOAT3(comp->w);
|
2013-09-09 21:52:26 +00:00
|
|
|
#endif
|
2015-09-23 07:23:37 +00:00
|
|
|
sc.weight = weight;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
prim->setup();
|
2013-11-29 16:17:00 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
switch(prim->category) {
|
|
|
|
case CClosurePrimitive::BSDF: {
|
|
|
|
CBSDFClosure *bsdf = (CBSDFClosure *)prim;
|
|
|
|
int scattering = bsdf->scattering();
|
Cycles: Add multi-scattering, energy-conserving GGX as an option to the Glossy, Anisotropic and Glass BSDFs
This commit adds a new distribution to the Glossy, Anisotropic and Glass BSDFs that implements the
multiple-scattering microfacet model described in the paper "Multiple-Scattering Microfacet BSDFs with the Smith Model".
Essentially, the improvement is that unlike classical GGX, which only models single scattering and assumes
the contribution of multiple bounces to be zero, this new model performs a random walk on the microsurface until
the ray leaves it again, which ensures perfect energy conservation.
In practise, this means that the "darkening problem" - GGX materials becoming darker with increasing
roughness - is solved in a physically correct and efficient way.
The downside of this model is that it has no (known) analytic expression for evalation. However, it can be
evaluated stochastically, and although the correct PDF isn't known either, the properties of MIS and the
balance heuristic guarantee an unbiased result at the cost of slightly higher noise.
Reviewers: dingto, #cycles, brecht
Reviewed By: dingto, #cycles, brecht
Subscribers: bliblubli, ace_dragon, gregzaal, brecht, harvester, dingto, marcog, swerner, jtheninja, Blendify, nutel
Differential Revision: https://developer.blender.org/D2002
2016-06-23 20:56:43 +00:00
|
|
|
int shaderdata_flag = bsdf->shaderdata_flag();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
/* caustic options */
|
|
|
|
if((scattering & LABEL_GLOSSY) && (path_flag & PATH_RAY_DIFFUSE)) {
|
|
|
|
KernelGlobals *kg = sd->osl_globals;
|
2014-09-05 18:39:35 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
if((!kernel_data.integrator.caustics_reflective && (scattering & LABEL_REFLECT)) ||
|
|
|
|
(!kernel_data.integrator.caustics_refractive && (scattering & LABEL_TRANSMIT)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-09-05 18:39:35 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
/* sample weight */
|
|
|
|
float sample_weight = fabsf(average(weight));
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
sc.sample_weight = sample_weight;
|
2012-12-15 10:18:42 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
sc.type = bsdf->sc.type;
|
|
|
|
sc.N = bsdf->sc.N;
|
|
|
|
sc.T = bsdf->sc.T;
|
|
|
|
sc.data0 = bsdf->sc.data0;
|
|
|
|
sc.data1 = bsdf->sc.data1;
|
|
|
|
sc.data2 = bsdf->sc.data2;
|
|
|
|
sc.prim = bsdf->sc.prim;
|
Cycles: Add multi-scattering, energy-conserving GGX as an option to the Glossy, Anisotropic and Glass BSDFs
This commit adds a new distribution to the Glossy, Anisotropic and Glass BSDFs that implements the
multiple-scattering microfacet model described in the paper "Multiple-Scattering Microfacet BSDFs with the Smith Model".
Essentially, the improvement is that unlike classical GGX, which only models single scattering and assumes
the contribution of multiple bounces to be zero, this new model performs a random walk on the microsurface until
the ray leaves it again, which ensures perfect energy conservation.
In practise, this means that the "darkening problem" - GGX materials becoming darker with increasing
roughness - is solved in a physically correct and efficient way.
The downside of this model is that it has no (known) analytic expression for evalation. However, it can be
evaluated stochastically, and although the correct PDF isn't known either, the properties of MIS and the
balance heuristic guarantee an unbiased result at the cost of slightly higher noise.
Reviewers: dingto, #cycles, brecht
Reviewed By: dingto, #cycles, brecht
Subscribers: bliblubli, ace_dragon, gregzaal, brecht, harvester, dingto, marcog, swerner, jtheninja, Blendify, nutel
Differential Revision: https://developer.blender.org/D2002
2016-06-23 20:56:43 +00:00
|
|
|
if(shaderdata_flag & SD_BSDF_HAS_CUSTOM) {
|
|
|
|
sc.custom1 = bsdf->sc.custom1;
|
|
|
|
sc.custom2 = bsdf->sc.custom2;
|
|
|
|
sc.custom3 = bsdf->sc.custom3;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
/* add */
|
|
|
|
if(sc.sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure < MAX_CLOSURE) {
|
|
|
|
sd->closure[sd->num_closure++] = sc;
|
Cycles: Add multi-scattering, energy-conserving GGX as an option to the Glossy, Anisotropic and Glass BSDFs
This commit adds a new distribution to the Glossy, Anisotropic and Glass BSDFs that implements the
multiple-scattering microfacet model described in the paper "Multiple-Scattering Microfacet BSDFs with the Smith Model".
Essentially, the improvement is that unlike classical GGX, which only models single scattering and assumes
the contribution of multiple bounces to be zero, this new model performs a random walk on the microsurface until
the ray leaves it again, which ensures perfect energy conservation.
In practise, this means that the "darkening problem" - GGX materials becoming darker with increasing
roughness - is solved in a physically correct and efficient way.
The downside of this model is that it has no (known) analytic expression for evalation. However, it can be
evaluated stochastically, and although the correct PDF isn't known either, the properties of MIS and the
balance heuristic guarantee an unbiased result at the cost of slightly higher noise.
Reviewers: dingto, #cycles, brecht
Reviewed By: dingto, #cycles, brecht
Subscribers: bliblubli, ace_dragon, gregzaal, brecht, harvester, dingto, marcog, swerner, jtheninja, Blendify, nutel
Differential Revision: https://developer.blender.org/D2002
2016-06-23 20:56:43 +00:00
|
|
|
sd->flag |= shaderdata_flag;
|
2015-09-23 07:23:37 +00:00
|
|
|
}
|
|
|
|
break;
|
2012-12-03 12:21:44 +00:00
|
|
|
}
|
2015-09-23 07:23:37 +00:00
|
|
|
case CClosurePrimitive::Emissive: {
|
|
|
|
/* sample weight */
|
|
|
|
float sample_weight = fabsf(average(weight));
|
2013-04-01 20:26:52 +00:00
|
|
|
|
|
|
|
sc.sample_weight = sample_weight;
|
2015-09-23 07:23:37 +00:00
|
|
|
sc.type = CLOSURE_EMISSION_ID;
|
|
|
|
sc.data0 = 0.0f;
|
|
|
|
sc.data1 = 0.0f;
|
|
|
|
sc.data2 = 0.0f;
|
2013-04-01 20:26:52 +00:00
|
|
|
sc.prim = NULL;
|
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
/* flag */
|
|
|
|
if(sd->num_closure < MAX_CLOSURE) {
|
2013-04-01 20:26:52 +00:00
|
|
|
sd->closure[sd->num_closure++] = sc;
|
2015-09-23 07:23:37 +00:00
|
|
|
sd->flag |= SD_EMISSION;
|
2013-04-01 20:26:52 +00:00
|
|
|
}
|
2015-09-23 07:23:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CClosurePrimitive::AmbientOcclusion: {
|
|
|
|
/* sample weight */
|
|
|
|
float sample_weight = fabsf(average(weight));
|
2013-04-01 20:26:52 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
sc.sample_weight = sample_weight;
|
|
|
|
sc.type = CLOSURE_AMBIENT_OCCLUSION_ID;
|
|
|
|
sc.data0 = 0.0f;
|
|
|
|
sc.data1 = 0.0f;
|
|
|
|
sc.data2 = 0.0f;
|
|
|
|
sc.prim = NULL;
|
|
|
|
|
|
|
|
if(sd->num_closure < MAX_CLOSURE) {
|
2013-04-01 20:26:52 +00:00
|
|
|
sd->closure[sd->num_closure++] = sc;
|
2015-09-23 07:23:37 +00:00
|
|
|
sd->flag |= SD_AO;
|
2013-04-01 20:26:52 +00:00
|
|
|
}
|
2015-09-23 07:23:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CClosurePrimitive::Holdout: {
|
|
|
|
sc.sample_weight = 0.0f;
|
|
|
|
sc.type = CLOSURE_HOLDOUT_ID;
|
|
|
|
sc.data0 = 0.0f;
|
|
|
|
sc.data1 = 0.0f;
|
|
|
|
sc.data2 = 0.0f;
|
|
|
|
sc.prim = NULL;
|
2013-04-01 20:26:52 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
if(sd->num_closure < MAX_CLOSURE) {
|
2013-04-01 20:26:52 +00:00
|
|
|
sd->closure[sd->num_closure++] = sc;
|
2015-09-23 07:23:37 +00:00
|
|
|
sd->flag |= SD_HOLDOUT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CClosurePrimitive::BSSRDF: {
|
|
|
|
CBSSRDFClosure *bssrdf = (CBSSRDFClosure *)prim;
|
|
|
|
float sample_weight = fabsf(average(weight));
|
|
|
|
|
|
|
|
if(sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure+2 < MAX_CLOSURE) {
|
|
|
|
sc.sample_weight = sample_weight;
|
|
|
|
|
|
|
|
sc.type = bssrdf->sc.type;
|
|
|
|
sc.N = bssrdf->sc.N;
|
|
|
|
sc.data1 = bssrdf->sc.data1;
|
|
|
|
sc.T.x = bssrdf->sc.T.x;
|
|
|
|
sc.prim = NULL;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR)
|
|
|
|
bssrdf->radius = make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
|
2016-02-03 22:34:49 +00:00
|
|
|
float3 albedo =
|
|
|
|
(bssrdf->sc.type == CLOSURE_BSSRDF_BURLEY_ID)
|
|
|
|
? bssrdf->albedo
|
|
|
|
: make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
/* create one closure for each color channel */
|
|
|
|
if(fabsf(weight.x) > 0.0f) {
|
|
|
|
sc.weight = make_float3(weight.x, 0.0f, 0.0f);
|
|
|
|
sc.data0 = bssrdf->radius.x;
|
|
|
|
sc.data1 = 0.0f;
|
2016-02-03 22:34:49 +00:00
|
|
|
sc.data2 = albedo.x;
|
2015-09-23 07:23:37 +00:00
|
|
|
sd->flag |= bssrdf_setup(&sc, sc.type);
|
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fabsf(weight.y) > 0.0f) {
|
|
|
|
sc.weight = make_float3(0.0f, weight.y, 0.0f);
|
|
|
|
sc.data0 = bssrdf->radius.y;
|
|
|
|
sc.data1 = 0.0f;
|
2016-02-03 22:34:49 +00:00
|
|
|
sc.data2 = albedo.y;
|
2015-09-23 07:23:37 +00:00
|
|
|
sd->flag |= bssrdf_setup(&sc, sc.type);
|
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fabsf(weight.z) > 0.0f) {
|
|
|
|
sc.weight = make_float3(0.0f, 0.0f, weight.z);
|
|
|
|
sc.data0 = bssrdf->radius.z;
|
|
|
|
sc.data1 = 0.0f;
|
2016-02-03 22:34:49 +00:00
|
|
|
sc.data2 = albedo.z;
|
2015-09-23 07:23:37 +00:00
|
|
|
sd->flag |= bssrdf_setup(&sc, sc.type);
|
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
}
|
2013-04-01 20:26:52 +00:00
|
|
|
}
|
2015-09-23 07:23:37 +00:00
|
|
|
break;
|
2013-04-01 20:26:52 +00:00
|
|
|
}
|
2015-09-23 07:23:37 +00:00
|
|
|
case CClosurePrimitive::Background:
|
|
|
|
case CClosurePrimitive::Volume:
|
|
|
|
break; /* not relevant */
|
2013-04-01 20:26:52 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2015-09-23 07:23:37 +00:00
|
|
|
break;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 22:38:13 +00:00
|
|
|
void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, ShaderContext ctx)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* setup shader globals from shader data */
|
2012-12-15 10:18:42 +00:00
|
|
|
OSLThreadData *tdata = kg->osl_tdata;
|
2016-01-06 22:38:13 +00:00
|
|
|
shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* execute shader for this point */
|
2012-12-15 10:18:42 +00:00
|
|
|
OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
|
|
|
|
OSL::ShaderGlobals *globals = &tdata->globals;
|
|
|
|
OSL::ShadingContext *octx = tdata->context[(int)ctx];
|
2011-09-27 20:37:24 +00:00
|
|
|
int shader = sd->shader & SHADER_MASK;
|
|
|
|
|
2016-01-07 09:04:12 +00:00
|
|
|
if(kg->osl->surface_state[shader]) {
|
|
|
|
ss->execute(octx, *(kg->osl->surface_state[shader]), *globals);
|
|
|
|
}
|
Cycles OSL: support for the trace(point pos, vector dir, ...) function, to trace
rays from the OSL shader. The "shade" parameter is not supported currently, but
attributes can be retrieved from the object that was hit using the
getmessage("trace", ..) function.
As mentioned in the OSL specification, this function can't be used instead of
lighting, the main purpose is to allow shaders to "probe" nearby geometry, for
example to apply a projected texture that can be blocked by geometry, apply
more “wear” to exposed geometry, or make other ambient occlusion-like effects.
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/OSL#Trace
Example .blend and render:
http://www.pasteall.org/blend/17347
http://www.pasteall.org/pic/show.php?id=40066
2012-11-06 19:59:10 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* flatten closure tree */
|
2015-03-27 19:15:15 +00:00
|
|
|
if(globals->Ci)
|
2013-08-18 14:15:57 +00:00
|
|
|
flatten_surface_closure_tree(sd, path_flag, globals->Ci);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Background */
|
|
|
|
|
|
|
|
static float3 flatten_background_closure_tree(const OSL::ClosureColor *closure)
|
|
|
|
{
|
2012-06-05 09:29:47 +00:00
|
|
|
/* OSL gives us a closure tree, if we are shading for background there
|
2011-04-27 11:58:34 +00:00
|
|
|
* is only one supported closure type at the moment, which has no evaluation
|
|
|
|
* functions, so we just sum the weights */
|
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
switch(closure->id) {
|
|
|
|
case OSL::ClosureColor::MUL: {
|
|
|
|
OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
return TO_FLOAT3(mul->weight) * flatten_background_closure_tree(mul->closure);
|
|
|
|
}
|
|
|
|
case OSL::ClosureColor::ADD: {
|
|
|
|
OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
return flatten_background_closure_tree(add->closureA) +
|
|
|
|
flatten_background_closure_tree(add->closureB);
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
|
|
|
|
CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
|
|
|
|
|
|
|
|
if(prim && prim->category == CClosurePrimitive::Background)
|
|
|
|
#ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
|
|
|
|
return TO_FLOAT3(comp->w);
|
|
|
|
#else
|
|
|
|
return make_float3(1.0f, 1.0f, 1.0f);
|
|
|
|
#endif
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
2016-01-06 22:38:13 +00:00
|
|
|
float3 OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, ShaderContext ctx)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* setup shader globals from shader data */
|
2012-12-15 10:18:42 +00:00
|
|
|
OSLThreadData *tdata = kg->osl_tdata;
|
2016-01-06 22:38:13 +00:00
|
|
|
shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* execute shader for this point */
|
2012-12-15 10:18:42 +00:00
|
|
|
OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
|
|
|
|
OSL::ShaderGlobals *globals = &tdata->globals;
|
|
|
|
OSL::ShadingContext *octx = tdata->context[(int)ctx];
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2016-01-07 09:04:12 +00:00
|
|
|
if(kg->osl->background_state) {
|
|
|
|
ss->execute(octx, *(kg->osl->background_state), *globals);
|
|
|
|
}
|
Cycles OSL: support for the trace(point pos, vector dir, ...) function, to trace
rays from the OSL shader. The "shade" parameter is not supported currently, but
attributes can be retrieved from the object that was hit using the
getmessage("trace", ..) function.
As mentioned in the OSL specification, this function can't be used instead of
lighting, the main purpose is to allow shaders to "probe" nearby geometry, for
example to apply a projected texture that can be blocked by geometry, apply
more “wear” to exposed geometry, or make other ambient occlusion-like effects.
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/OSL#Trace
Example .blend and render:
http://www.pasteall.org/blend/17347
http://www.pasteall.org/pic/show.php?id=40066
2012-11-06 19:59:10 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* return background color immediately */
|
2015-03-27 19:15:15 +00:00
|
|
|
if(globals->Ci)
|
2011-04-27 11:58:34 +00:00
|
|
|
return flatten_background_closure_tree(globals->Ci);
|
|
|
|
|
|
|
|
return make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Volume */
|
|
|
|
|
|
|
|
static void flatten_volume_closure_tree(ShaderData *sd,
|
2012-06-04 22:44:58 +00:00
|
|
|
const OSL::ClosureColor *closure, float3 weight = make_float3(1.0f, 1.0f, 1.0f))
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-06-05 09:29:47 +00:00
|
|
|
/* OSL gives us a closure tree, we flatten it into arrays per
|
2011-04-27 11:58:34 +00:00
|
|
|
* closure type, for evaluation, sampling, etc later on. */
|
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
switch(closure->id) {
|
|
|
|
case OSL::ClosureColor::MUL: {
|
|
|
|
OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
|
|
|
|
flatten_volume_closure_tree(sd, mul->closure, TO_FLOAT3(mul->weight) * weight);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OSL::ClosureColor::ADD: {
|
|
|
|
OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
|
|
|
|
flatten_volume_closure_tree(sd, add->closureA, weight);
|
|
|
|
flatten_volume_closure_tree(sd, add->closureB, weight);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
|
|
|
|
CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-09-23 07:23:37 +00:00
|
|
|
if(prim) {
|
|
|
|
ShaderClosure sc;
|
2013-12-18 11:29:11 +00:00
|
|
|
|
2013-09-09 21:52:26 +00:00
|
|
|
#ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
|
2015-09-23 07:23:37 +00:00
|
|
|
weight = weight*TO_FLOAT3(comp->w);
|
2013-09-09 21:52:26 +00:00
|
|
|
#endif
|
2015-09-23 07:23:37 +00:00
|
|
|
sc.weight = weight;
|
|
|
|
|
|
|
|
prim->setup();
|
|
|
|
|
|
|
|
switch(prim->category) {
|
|
|
|
case CClosurePrimitive::Volume: {
|
|
|
|
CVolumeClosure *volume = (CVolumeClosure *)prim;
|
|
|
|
/* sample weight */
|
|
|
|
float sample_weight = fabsf(average(weight));
|
|
|
|
|
|
|
|
sc.sample_weight = sample_weight;
|
|
|
|
sc.type = volume->sc.type;
|
|
|
|
sc.data0 = volume->sc.data0;
|
|
|
|
sc.data1 = volume->sc.data1;
|
|
|
|
|
|
|
|
/* add */
|
|
|
|
if((sc.sample_weight > CLOSURE_WEIGHT_CUTOFF) &&
|
|
|
|
(sd->num_closure < MAX_CLOSURE))
|
|
|
|
{
|
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
sd->flag |= volume->shaderdata_flag();
|
|
|
|
}
|
|
|
|
break;
|
2013-12-28 00:54:44 +00:00
|
|
|
}
|
2015-09-23 07:23:37 +00:00
|
|
|
case CClosurePrimitive::Emissive: {
|
|
|
|
/* sample weight */
|
|
|
|
float sample_weight = fabsf(average(weight));
|
|
|
|
|
|
|
|
sc.sample_weight = sample_weight;
|
|
|
|
sc.type = CLOSURE_EMISSION_ID;
|
|
|
|
sc.data0 = 0.0f;
|
|
|
|
sc.data1 = 0.0f;
|
|
|
|
sc.prim = NULL;
|
|
|
|
|
|
|
|
/* flag */
|
|
|
|
if(sd->num_closure < MAX_CLOSURE) {
|
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
sd->flag |= SD_EMISSION;
|
|
|
|
}
|
|
|
|
break;
|
2013-12-29 15:30:55 +00:00
|
|
|
}
|
2015-09-23 07:23:37 +00:00
|
|
|
case CClosurePrimitive::Holdout:
|
|
|
|
break; /* not implemented */
|
|
|
|
case CClosurePrimitive::Background:
|
|
|
|
case CClosurePrimitive::BSDF:
|
|
|
|
case CClosurePrimitive::BSSRDF:
|
|
|
|
case CClosurePrimitive::AmbientOcclusion:
|
|
|
|
break; /* not relevant */
|
2013-12-29 15:30:55 +00:00
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 22:38:13 +00:00
|
|
|
void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, ShaderContext ctx)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* setup shader globals from shader data */
|
2012-12-15 10:18:42 +00:00
|
|
|
OSLThreadData *tdata = kg->osl_tdata;
|
2016-01-06 22:38:13 +00:00
|
|
|
shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* execute shader */
|
2012-12-15 10:18:42 +00:00
|
|
|
OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
|
|
|
|
OSL::ShaderGlobals *globals = &tdata->globals;
|
|
|
|
OSL::ShadingContext *octx = tdata->context[(int)ctx];
|
2011-09-27 20:37:24 +00:00
|
|
|
int shader = sd->shader & SHADER_MASK;
|
|
|
|
|
2016-01-07 09:04:12 +00:00
|
|
|
if(kg->osl->volume_state[shader]) {
|
|
|
|
ss->execute(octx, *(kg->osl->volume_state[shader]), *globals);
|
|
|
|
}
|
2013-12-28 00:54:44 +00:00
|
|
|
|
|
|
|
/* flatten closure tree */
|
2015-03-27 19:15:15 +00:00
|
|
|
if(globals->Ci)
|
2011-04-27 11:58:34 +00:00
|
|
|
flatten_volume_closure_tree(sd, globals->Ci);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Displacement */
|
|
|
|
|
2012-12-15 10:18:42 +00:00
|
|
|
void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd, ShaderContext ctx)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
/* setup shader globals from shader data */
|
2012-12-15 10:18:42 +00:00
|
|
|
OSLThreadData *tdata = kg->osl_tdata;
|
2016-01-06 22:38:13 +00:00
|
|
|
|
|
|
|
PathState state = {0};
|
|
|
|
|
|
|
|
shaderdata_to_shaderglobals(kg, sd, &state, 0, tdata);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* execute shader */
|
2012-12-15 10:18:42 +00:00
|
|
|
OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
|
|
|
|
OSL::ShaderGlobals *globals = &tdata->globals;
|
|
|
|
OSL::ShadingContext *octx = tdata->context[(int)ctx];
|
2011-09-27 20:37:24 +00:00
|
|
|
int shader = sd->shader & SHADER_MASK;
|
|
|
|
|
2016-01-07 09:04:12 +00:00
|
|
|
if(kg->osl->displacement_state[shader]) {
|
|
|
|
ss->execute(octx, *(kg->osl->displacement_state[shader]), *globals);
|
|
|
|
}
|
Cycles OSL: support for the trace(point pos, vector dir, ...) function, to trace
rays from the OSL shader. The "shade" parameter is not supported currently, but
attributes can be retrieved from the object that was hit using the
getmessage("trace", ..) function.
As mentioned in the OSL specification, this function can't be used instead of
lighting, the main purpose is to allow shaders to "probe" nearby geometry, for
example to apply a projected texture that can be blocked by geometry, apply
more “wear” to exposed geometry, or make other ambient occlusion-like effects.
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/OSL#Trace
Example .blend and render:
http://www.pasteall.org/blend/17347
http://www.pasteall.org/pic/show.php?id=40066
2012-11-06 19:59:10 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* get back position */
|
|
|
|
sd->P = TO_FLOAT3(globals->P);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BSDF Closure */
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
int OSLShader::bsdf_sample(const ShaderData *sd, const ShaderClosure *sc, float randu, float randv, float3& eval, float3& omega_in, differential3& domega_in, float& pdf)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-10-20 12:18:00 +00:00
|
|
|
CBSDFClosure *sample_bsdf = (CBSDFClosure *)sc->prim;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
pdf = 0.0f;
|
|
|
|
|
2012-10-20 12:18:00 +00:00
|
|
|
return sample_bsdf->sample(sd->Ng,
|
|
|
|
sd->I, sd->dI.dx, sd->dI.dy,
|
|
|
|
randu, randv,
|
|
|
|
omega_in, domega_in.dx, domega_in.dy,
|
|
|
|
pdf, eval);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
float3 OSLShader::bsdf_eval(const ShaderData *sd, const ShaderClosure *sc, const float3& omega_in, float& pdf)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-10-20 12:18:00 +00:00
|
|
|
CBSDFClosure *bsdf = (CBSDFClosure *)sc->prim;
|
|
|
|
float3 bsdf_eval;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2015-03-27 19:15:15 +00:00
|
|
|
if(dot(sd->Ng, omega_in) >= 0.0f)
|
2012-10-20 12:18:00 +00:00
|
|
|
bsdf_eval = bsdf->eval_reflect(sd->I, omega_in, pdf);
|
2011-09-12 13:13:56 +00:00
|
|
|
else
|
2012-10-20 12:18:00 +00:00
|
|
|
bsdf_eval = bsdf->eval_transmit(sd->I, omega_in, pdf);
|
2011-09-12 13:13:56 +00:00
|
|
|
|
2012-10-20 12:18:00 +00:00
|
|
|
return bsdf_eval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OSLShader::bsdf_blur(ShaderClosure *sc, float roughness)
|
|
|
|
{
|
|
|
|
CBSDFClosure *bsdf = (CBSDFClosure *)sc->prim;
|
|
|
|
bsdf->blur(roughness);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
/* Attributes */
|
|
|
|
|
2013-01-03 12:08:54 +00:00
|
|
|
int OSLShader::find_attribute(KernelGlobals *kg, const ShaderData *sd, uint id, AttributeElement *elem)
|
2012-12-01 19:15:05 +00:00
|
|
|
{
|
|
|
|
/* for OSL, a hash map is used to lookup the attribute by name. */
|
2013-01-09 21:09:20 +00:00
|
|
|
int object = sd->object*ATTR_PRIM_TYPES;
|
|
|
|
#ifdef __HAIR__
|
2014-03-29 12:03:46 +00:00
|
|
|
if(sd->type & PRIMITIVE_ALL_CURVE) object += ATTR_PRIM_CURVE;
|
2013-01-09 21:09:20 +00:00
|
|
|
#endif
|
|
|
|
|
2013-01-03 12:08:54 +00:00
|
|
|
OSLGlobals::AttributeMap &attr_map = kg->osl->attribute_map[object];
|
|
|
|
ustring stdname(std::string("geom:") + std::string(Attribute::standard_name((AttributeStandard)id)));
|
2012-12-01 19:15:05 +00:00
|
|
|
OSLGlobals::AttributeMap::const_iterator it = attr_map.find(stdname);
|
|
|
|
|
2015-03-27 19:15:15 +00:00
|
|
|
if(it != attr_map.end()) {
|
2012-12-01 19:15:05 +00:00
|
|
|
const OSLGlobals::Attribute &osl_attr = it->second;
|
2013-01-03 12:08:54 +00:00
|
|
|
*elem = osl_attr.elem;
|
2013-12-31 16:33:55 +00:00
|
|
|
|
2014-03-29 12:03:47 +00:00
|
|
|
if(sd->prim == PRIM_NONE && (AttributeElement)osl_attr.elem != ATTR_ELEMENT_MESH)
|
2013-12-31 16:33:55 +00:00
|
|
|
return ATTR_STD_NOT_FOUND;
|
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
/* return result */
|
|
|
|
return (osl_attr.elem == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND : osl_attr.offset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (int)ATTR_STD_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|