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
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
#include <OSL/oslexec.h>
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
static void shaderdata_to_shaderglobals(KernelGlobals *kg, ShaderData *sd,
|
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;
|
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. */
|
|
|
|
|
2012-06-04 22:44:58 +00:00
|
|
|
if (closure->type == OSL::ClosureColor::COMPONENT) {
|
|
|
|
OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
|
2013-11-29 01:09:24 +00:00
|
|
|
CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-06-04 22:44:58 +00:00
|
|
|
if (prim) {
|
2011-09-12 13:13:56 +00:00
|
|
|
ShaderClosure sc;
|
2013-12-18 11:29:11 +00:00
|
|
|
|
2013-09-09 21:52:26 +00:00
|
|
|
#ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
|
2013-12-18 11:29:11 +00:00
|
|
|
weight = weight*TO_FLOAT3(comp->w);
|
2013-09-09 21:52:26 +00:00
|
|
|
#endif
|
2013-12-18 11:29:11 +00:00
|
|
|
sc.weight = weight;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-11-29 16:17:00 +00:00
|
|
|
prim->setup();
|
|
|
|
|
2013-11-29 01:09:24 +00:00
|
|
|
switch (prim->category) {
|
|
|
|
case CClosurePrimitive::BSDF: {
|
2012-10-20 12:18:00 +00:00
|
|
|
CBSDFClosure *bsdf = (CBSDFClosure *)prim;
|
|
|
|
int scattering = bsdf->scattering();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* no caustics option */
|
2013-08-18 14:15:57 +00:00
|
|
|
if(scattering == LABEL_GLOSSY && (path_flag & PATH_RAY_DIFFUSE)) {
|
|
|
|
KernelGlobals *kg = sd->osl_globals;
|
|
|
|
if(kernel_data.integrator.no_caustics)
|
|
|
|
return;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* sample weight */
|
2012-10-20 12:18:00 +00:00
|
|
|
float sample_weight = fabsf(average(weight));
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-10-20 12:18:00 +00:00
|
|
|
sc.sample_weight = sample_weight;
|
2012-12-15 10:18:42 +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;
|
2014-06-03 22:39:42 +00:00
|
|
|
sc.data2 = bsdf->sc.data2;
|
2012-12-15 10:18:42 +00:00
|
|
|
sc.prim = bsdf->sc.prim;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* add */
|
2013-12-28 00:54:44 +00:00
|
|
|
if(sc.sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure < MAX_CLOSURE) {
|
2012-12-03 12:21:44 +00:00
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
sd->flag |= bsdf->shaderdata_flag();
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-11-29 01:09:24 +00:00
|
|
|
case CClosurePrimitive::Emissive: {
|
2011-04-27 11:58:34 +00:00
|
|
|
/* sample weight */
|
|
|
|
float sample_weight = fabsf(average(weight));
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
sc.sample_weight = sample_weight;
|
|
|
|
sc.type = CLOSURE_EMISSION_ID;
|
2013-08-20 18:25:59 +00:00
|
|
|
sc.data0 = 0.0f;
|
|
|
|
sc.data1 = 0.0f;
|
2014-06-03 22:39:42 +00:00
|
|
|
sc.data2 = 0.0f;
|
2012-12-15 10:18:42 +00:00
|
|
|
sc.prim = NULL;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* flag */
|
2012-12-03 12:21:44 +00:00
|
|
|
if(sd->num_closure < MAX_CLOSURE) {
|
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
sd->flag |= SD_EMISSION;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-11-29 01:09:24 +00:00
|
|
|
case CClosurePrimitive::AmbientOcclusion: {
|
2012-11-06 19:59:02 +00:00
|
|
|
/* sample weight */
|
|
|
|
float sample_weight = fabsf(average(weight));
|
|
|
|
|
|
|
|
sc.sample_weight = sample_weight;
|
|
|
|
sc.type = CLOSURE_AMBIENT_OCCLUSION_ID;
|
2013-08-20 18:25:59 +00:00
|
|
|
sc.data0 = 0.0f;
|
|
|
|
sc.data1 = 0.0f;
|
2014-06-03 22:39:42 +00:00
|
|
|
sc.data2 = 0.0f;
|
2012-12-15 10:18:42 +00:00
|
|
|
sc.prim = NULL;
|
2012-11-06 19:59:02 +00:00
|
|
|
|
2012-12-03 12:21:44 +00:00
|
|
|
if(sd->num_closure < MAX_CLOSURE) {
|
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
sd->flag |= SD_AO;
|
|
|
|
}
|
2012-11-06 19:59:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-11-29 01:09:24 +00:00
|
|
|
case CClosurePrimitive::Holdout: {
|
2011-09-12 13:13:56 +00:00
|
|
|
sc.sample_weight = 0.0f;
|
|
|
|
sc.type = CLOSURE_HOLDOUT_ID;
|
2013-08-20 18:25:59 +00:00
|
|
|
sc.data0 = 0.0f;
|
|
|
|
sc.data1 = 0.0f;
|
2014-06-03 22:39:42 +00:00
|
|
|
sc.data2 = 0.0f;
|
2012-12-15 10:18:42 +00:00
|
|
|
sc.prim = NULL;
|
2012-12-03 12:21:44 +00:00
|
|
|
|
|
|
|
if(sd->num_closure < MAX_CLOSURE) {
|
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
sd->flag |= SD_HOLDOUT;
|
|
|
|
}
|
2011-08-28 13:55:59 +00:00
|
|
|
break;
|
2013-04-01 20:26:52 +00:00
|
|
|
}
|
2013-11-29 01:09:24 +00:00
|
|
|
case CClosurePrimitive::BSSRDF: {
|
2013-04-01 20:26:52 +00:00
|
|
|
CBSSRDFClosure *bssrdf = (CBSSRDFClosure *)prim;
|
|
|
|
float sample_weight = fabsf(average(weight));
|
|
|
|
|
2013-12-28 00:54:44 +00:00
|
|
|
if(sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure+2 < MAX_CLOSURE) {
|
2013-04-01 20:26:52 +00:00
|
|
|
sc.sample_weight = sample_weight;
|
|
|
|
|
|
|
|
sc.type = bssrdf->sc.type;
|
|
|
|
sc.N = bssrdf->sc.N;
|
|
|
|
sc.data1 = bssrdf->sc.data1;
|
2013-09-03 22:39:17 +00:00
|
|
|
sc.T.x = bssrdf->sc.T.x;
|
2013-04-01 20:26:52 +00:00
|
|
|
sc.prim = NULL;
|
|
|
|
|
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
|
|
|
bssrdf->radius = make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
|
2013-04-01 20:26:52 +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;
|
2013-08-18 14:15:57 +00:00
|
|
|
sd->flag |= bssrdf_setup(&sc, sc.type);
|
2013-04-01 20:26:52 +00:00
|
|
|
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;
|
2013-08-18 14:15:57 +00:00
|
|
|
sd->flag |= bssrdf_setup(&sc, sc.type);
|
2013-04-01 20:26:52 +00:00
|
|
|
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;
|
2013-08-18 14:15:57 +00:00
|
|
|
sd->flag |= bssrdf_setup(&sc, sc.type);
|
2013-04-01 20:26:52 +00:00
|
|
|
sd->closure[sd->num_closure++] = sc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-11-29 01:09:24 +00:00
|
|
|
case CClosurePrimitive::Background:
|
|
|
|
case CClosurePrimitive::Volume:
|
2011-04-27 11:58:34 +00:00
|
|
|
break; /* not relevant */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 22:44:58 +00:00
|
|
|
else if (closure->type == OSL::ClosureColor::MUL) {
|
|
|
|
OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
|
2013-08-18 14:15:57 +00:00
|
|
|
flatten_surface_closure_tree(sd, path_flag, mul->closure, TO_FLOAT3(mul->weight) * weight);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2012-06-04 22:44:58 +00:00
|
|
|
else if (closure->type == OSL::ClosureColor::ADD) {
|
|
|
|
OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
|
2013-08-18 14:15:57 +00:00
|
|
|
flatten_surface_closure_tree(sd, path_flag, add->closureA, weight);
|
|
|
|
flatten_surface_closure_tree(sd, path_flag, add->closureB, weight);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 19:02:40 +00:00
|
|
|
void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, 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;
|
|
|
|
shaderdata_to_shaderglobals(kg, sd, 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;
|
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
if (kg->osl->surface_state[shader])
|
2012-12-15 10:18:42 +00:00
|
|
|
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 */
|
2013-08-18 14:15:57 +00:00
|
|
|
if (globals->Ci)
|
|
|
|
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 */
|
|
|
|
|
2012-06-04 22:44:58 +00:00
|
|
|
if (closure->type == OSL::ClosureColor::COMPONENT) {
|
|
|
|
OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
|
2013-11-29 01:09:24 +00:00
|
|
|
CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-11-29 01:09:24 +00:00
|
|
|
if (prim && prim->category == CClosurePrimitive::Background)
|
2013-09-09 21:52:26 +00:00
|
|
|
#ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
|
|
|
|
return TO_FLOAT3(comp->w);
|
|
|
|
#else
|
2011-04-27 11:58:34 +00:00
|
|
|
return make_float3(1.0f, 1.0f, 1.0f);
|
2013-09-09 21:52:26 +00:00
|
|
|
#endif
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
2012-06-04 22:44:58 +00:00
|
|
|
else if (closure->type == OSL::ClosureColor::MUL) {
|
|
|
|
OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return TO_FLOAT3(mul->weight) * flatten_background_closure_tree(mul->closure);
|
|
|
|
}
|
2012-06-04 22:44:58 +00:00
|
|
|
else if (closure->type == OSL::ClosureColor::ADD) {
|
|
|
|
OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
return flatten_background_closure_tree(add->closureA) +
|
2012-06-04 22:44:58 +00:00
|
|
|
flatten_background_closure_tree(add->closureB);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
2012-12-15 10:18:42 +00:00
|
|
|
float3 OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, 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;
|
|
|
|
shaderdata_to_shaderglobals(kg, sd, 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
|
|
|
|
2012-12-15 10:18:42 +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 */
|
2012-06-04 22:44:58 +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. */
|
|
|
|
|
2012-06-04 22:44:58 +00:00
|
|
|
if (closure->type == OSL::ClosureColor::COMPONENT) {
|
|
|
|
OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
|
2013-11-29 01:09:24 +00:00
|
|
|
CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-06-04 22:44:58 +00:00
|
|
|
if (prim) {
|
2011-09-12 13:13:56 +00:00
|
|
|
ShaderClosure sc;
|
2013-12-18 11:29:11 +00:00
|
|
|
|
2013-09-09 21:52:26 +00:00
|
|
|
#ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
|
2013-12-18 11:29:11 +00:00
|
|
|
weight = weight*TO_FLOAT3(comp->w);
|
2013-09-09 21:52:26 +00:00
|
|
|
#endif
|
2013-12-18 11:29:11 +00:00
|
|
|
sc.weight = weight;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2013-11-29 16:17:00 +00:00
|
|
|
prim->setup();
|
|
|
|
|
2013-11-29 01:09:24 +00:00
|
|
|
switch (prim->category) {
|
|
|
|
case CClosurePrimitive::Volume: {
|
2013-12-28 00:54:44 +00:00
|
|
|
CVolumeClosure *volume = (CVolumeClosure *)prim;
|
2011-04-27 11:58:34 +00:00
|
|
|
/* sample weight */
|
|
|
|
float sample_weight = fabsf(average(weight));
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
sc.sample_weight = sample_weight;
|
2013-12-28 00:54:44 +00:00
|
|
|
sc.type = volume->sc.type;
|
|
|
|
sc.data0 = volume->sc.data0;
|
|
|
|
sc.data1 = volume->sc.data1;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
/* add */
|
2014-05-04 16:19:08 +00:00
|
|
|
if((sc.sample_weight > CLOSURE_WEIGHT_CUTOFF) &&
|
|
|
|
(sd->num_closure < MAX_CLOSURE))
|
|
|
|
{
|
2012-12-03 12:21:44 +00:00
|
|
|
sd->closure[sd->num_closure++] = sc;
|
2013-12-28 00:54:44 +00:00
|
|
|
sd->flag |= volume->shaderdata_flag();
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-12-29 15:30:55 +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-11-29 01:09:24 +00:00
|
|
|
case CClosurePrimitive::Holdout:
|
2011-04-27 11:58:34 +00:00
|
|
|
break; /* not implemented */
|
2013-11-29 01:09:24 +00:00
|
|
|
case CClosurePrimitive::Background:
|
|
|
|
case CClosurePrimitive::BSDF:
|
|
|
|
case CClosurePrimitive::BSSRDF:
|
|
|
|
case CClosurePrimitive::AmbientOcclusion:
|
2011-04-27 11:58:34 +00:00
|
|
|
break; /* not relevant */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-04 22:44:58 +00:00
|
|
|
else if (closure->type == OSL::ClosureColor::MUL) {
|
|
|
|
OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
|
2011-04-27 11:58:34 +00:00
|
|
|
flatten_volume_closure_tree(sd, mul->closure, TO_FLOAT3(mul->weight) * weight);
|
|
|
|
}
|
2012-06-04 22:44:58 +00:00
|
|
|
else if (closure->type == OSL::ClosureColor::ADD) {
|
|
|
|
OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
|
2011-04-27 11:58:34 +00:00
|
|
|
flatten_volume_closure_tree(sd, add->closureA, weight);
|
|
|
|
flatten_volume_closure_tree(sd, add->closureB, weight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-28 19:02:40 +00:00
|
|
|
void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, 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;
|
|
|
|
shaderdata_to_shaderglobals(kg, sd, 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;
|
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
if (kg->osl->volume_state[shader])
|
2012-12-15 10:18:42 +00:00
|
|
|
ss->execute(*octx, *(kg->osl->volume_state[shader]), *globals);
|
2013-12-28 00:54:44 +00:00
|
|
|
|
|
|
|
/* flatten closure tree */
|
2012-06-04 22:44:58 +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;
|
|
|
|
shaderdata_to_shaderglobals(kg, sd, 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;
|
|
|
|
|
2012-12-01 19:15:05 +00:00
|
|
|
if (kg->osl->displacement_state[shader])
|
2012-12-15 10:18:42 +00:00
|
|
|
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
|
|
|
|
2012-06-04 22:44:58 +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);
|
|
|
|
|
|
|
|
if (it != attr_map.end()) {
|
|
|
|
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
|
|
|
|
|