Cycles: Code cleanup, spaces around keywords
[blender.git] / intern / cycles / kernel / osl / osl_shader.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <OSL/oslexec.h>
18
19 #include "kernel_compat_cpu.h"
20 #include "kernel_montecarlo.h"
21 #include "kernel_types.h"
22 #include "kernel_globals.h"
23
24 #include "geom/geom_object.h"
25
26 #include "closure/bsdf_diffuse.h"
27 #include "closure/bssrdf.h"
28
29 #include "osl_bssrdf.h"
30 #include "osl_closures.h"
31 #include "osl_globals.h"
32 #include "osl_services.h"
33 #include "osl_shader.h"
34
35 #include "util_foreach.h"
36
37 #include "attribute.h"
38
39
40 CCL_NAMESPACE_BEGIN
41
42 /* Threads */
43
44 void OSLShader::thread_init(KernelGlobals *kg, KernelGlobals *kernel_globals, OSLGlobals *osl_globals)
45 {
46         /* no osl used? */
47         if(!osl_globals->use) {
48                 kg->osl = NULL;
49                 return;
50         }
51
52         /* per thread kernel data init*/
53         kg->osl = osl_globals;
54         kg->osl->services->thread_init(kernel_globals, osl_globals->ts);
55
56         OSL::ShadingSystem *ss = kg->osl->ss;
57         OSLThreadData *tdata = new OSLThreadData();
58
59         memset(&tdata->globals, 0, sizeof(OSL::ShaderGlobals));
60         tdata->globals.tracedata = &tdata->tracedata;
61         tdata->globals.flipHandedness = false;
62         tdata->osl_thread_info = ss->create_thread_info();
63
64         for(int i = 0; i < SHADER_CONTEXT_NUM; i++)
65                 tdata->context[i] = ss->get_context(tdata->osl_thread_info);
66
67         tdata->oiio_thread_info = osl_globals->ts->get_perthread_info();
68
69         kg->osl_ss = (OSLShadingSystem*)ss;
70         kg->osl_tdata = tdata;
71 }
72
73 void OSLShader::thread_free(KernelGlobals *kg)
74 {
75         if(!kg->osl)
76                 return;
77
78         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
79         OSLThreadData *tdata = kg->osl_tdata;
80
81         for(int i = 0; i < SHADER_CONTEXT_NUM; i++)
82                 ss->release_context(tdata->context[i]);
83
84         ss->destroy_thread_info(tdata->osl_thread_info);
85
86         delete tdata;
87
88         kg->osl = NULL;
89         kg->osl_ss = NULL;
90         kg->osl_tdata = NULL;
91 }
92
93 /* Globals */
94
95 static void shaderdata_to_shaderglobals(KernelGlobals *kg, ShaderData *sd,
96                                         int path_flag, OSLThreadData *tdata)
97 {
98         OSL::ShaderGlobals *globals = &tdata->globals;
99
100         /* copy from shader data to shader globals */
101         globals->P = TO_VEC3(sd->P);
102         globals->dPdx = TO_VEC3(sd->dP.dx);
103         globals->dPdy = TO_VEC3(sd->dP.dy);
104         globals->I = TO_VEC3(sd->I);
105         globals->dIdx = TO_VEC3(sd->dI.dx);
106         globals->dIdy = TO_VEC3(sd->dI.dy);
107         globals->N = TO_VEC3(sd->N);
108         globals->Ng = TO_VEC3(sd->Ng);
109         globals->u = sd->u;
110         globals->dudx = sd->du.dx;
111         globals->dudy = sd->du.dy;
112         globals->v = sd->v;
113         globals->dvdx = sd->dv.dx;
114         globals->dvdy = sd->dv.dy;
115         globals->dPdu = TO_VEC3(sd->dPdu);
116         globals->dPdv = TO_VEC3(sd->dPdv);
117         globals->surfacearea = (sd->object == OBJECT_NONE) ? 1.0f : object_surface_area(kg, sd->object);
118         globals->time = sd->time;
119
120         /* booleans */
121         globals->raytype = path_flag;
122         globals->backfacing = (sd->flag & SD_BACKFACING);
123
124         /* shader data to be used in services callbacks */
125         globals->renderstate = sd; 
126
127         /* hacky, we leave it to services to fetch actual object matrix */
128         globals->shader2common = sd;
129         globals->object2common = sd;
130
131         /* must be set to NULL before execute */
132         globals->Ci = NULL;
133
134         /* clear trace data */
135         tdata->tracedata.init = false;
136
137         /* used by renderservices */
138         sd->osl_globals = kg;
139 }
140
141 /* Surface */
142
143 static void flatten_surface_closure_tree(ShaderData *sd, int path_flag,
144                                          const OSL::ClosureColor *closure, float3 weight = make_float3(1.0f, 1.0f, 1.0f))
145 {
146         /* OSL gives us a closure tree, we flatten it into arrays per
147          * closure type, for evaluation, sampling, etc later on. */
148
149         if(closure->type == OSL::ClosureColor::COMPONENT) {
150                 OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
151                 CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
152
153                 if(prim) {
154                         ShaderClosure sc;
155
156 #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
157                         weight = weight*TO_FLOAT3(comp->w);
158 #endif
159                         sc.weight = weight;
160
161                         prim->setup();
162
163                         switch (prim->category) {
164                                 case CClosurePrimitive::BSDF: {
165                                         CBSDFClosure *bsdf = (CBSDFClosure *)prim;
166                                         int scattering = bsdf->scattering();
167
168                                         /* caustic options */
169                                         if((scattering & LABEL_GLOSSY) && (path_flag & PATH_RAY_DIFFUSE)) {
170                                                 KernelGlobals *kg = sd->osl_globals;
171
172                                                 if((!kernel_data.integrator.caustics_reflective && (scattering & LABEL_REFLECT)) ||
173                                                    (!kernel_data.integrator.caustics_refractive && (scattering & LABEL_TRANSMIT))) {
174                                                         return;
175                                                 }
176                                         }
177
178                                         /* sample weight */
179                                         float sample_weight = fabsf(average(weight));
180
181                                         sc.sample_weight = sample_weight;
182
183                                         sc.type = bsdf->sc.type;
184                                         sc.N = bsdf->sc.N;
185                                         sc.T = bsdf->sc.T;
186                                         sc.data0 = bsdf->sc.data0;
187                                         sc.data1 = bsdf->sc.data1;
188                                         sc.data2 = bsdf->sc.data2;
189                                         sc.prim = bsdf->sc.prim;
190
191                                         /* add */
192                                         if(sc.sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure < MAX_CLOSURE) {
193                                                 sd->closure[sd->num_closure++] = sc;
194                                                 sd->flag |= bsdf->shaderdata_flag();
195                                         }
196                                         break;
197                                 }
198                                 case CClosurePrimitive::Emissive: {
199                                         /* sample weight */
200                                         float sample_weight = fabsf(average(weight));
201
202                                         sc.sample_weight = sample_weight;
203                                         sc.type = CLOSURE_EMISSION_ID;
204                                         sc.data0 = 0.0f;
205                                         sc.data1 = 0.0f;
206                                         sc.data2 = 0.0f;
207                                         sc.prim = NULL;
208
209                                         /* flag */
210                                         if(sd->num_closure < MAX_CLOSURE) {
211                                                 sd->closure[sd->num_closure++] = sc;
212                                                 sd->flag |= SD_EMISSION;
213                                         }
214                                         break;
215                                 }
216                                 case CClosurePrimitive::AmbientOcclusion: {
217                                         /* sample weight */
218                                         float sample_weight = fabsf(average(weight));
219
220                                         sc.sample_weight = sample_weight;
221                                         sc.type = CLOSURE_AMBIENT_OCCLUSION_ID;
222                                         sc.data0 = 0.0f;
223                                         sc.data1 = 0.0f;
224                                         sc.data2 = 0.0f;
225                                         sc.prim = NULL;
226
227                                         if(sd->num_closure < MAX_CLOSURE) {
228                                                 sd->closure[sd->num_closure++] = sc;
229                                                 sd->flag |= SD_AO;
230                                         }
231                                         break;
232                                 }
233                                 case CClosurePrimitive::Holdout: {
234                                         sc.sample_weight = 0.0f;
235                                         sc.type = CLOSURE_HOLDOUT_ID;
236                                         sc.data0 = 0.0f;
237                                         sc.data1 = 0.0f;
238                                         sc.data2 = 0.0f;
239                                         sc.prim = NULL;
240
241                                         if(sd->num_closure < MAX_CLOSURE) {
242                                                 sd->closure[sd->num_closure++] = sc;
243                                                 sd->flag |= SD_HOLDOUT;
244                                         }
245                                         break;
246                                 }
247                                 case CClosurePrimitive::BSSRDF: {
248                                         CBSSRDFClosure *bssrdf = (CBSSRDFClosure *)prim;
249                                         float sample_weight = fabsf(average(weight));
250
251                                         if(sample_weight > CLOSURE_WEIGHT_CUTOFF && sd->num_closure+2 < MAX_CLOSURE) {
252                                                 sc.sample_weight = sample_weight;
253
254                                                 sc.type = bssrdf->sc.type;
255                                                 sc.N = bssrdf->sc.N;
256                                                 sc.data1 = bssrdf->sc.data1;
257                                                 sc.T.x = bssrdf->sc.T.x;
258                                                 sc.prim = NULL;
259
260                                                 /* disable in case of diffuse ancestor, can't see it well then and
261                                                  * adds considerably noise due to probabilities of continuing path
262                                                  * getting lower and lower */
263                                                 if(path_flag & PATH_RAY_DIFFUSE_ANCESTOR)
264                                                         bssrdf->radius = make_float3(0.0f, 0.0f, 0.0f);
265
266                                                 /* create one closure for each color channel */
267                                                 if(fabsf(weight.x) > 0.0f) {
268                                                         sc.weight = make_float3(weight.x, 0.0f, 0.0f);
269                                                         sc.data0 = bssrdf->radius.x;
270                                                         sc.data1 = 0.0f;
271                                                         sd->flag |= bssrdf_setup(&sc, sc.type);
272                                                         sd->closure[sd->num_closure++] = sc;
273                                                 }
274
275                                                 if(fabsf(weight.y) > 0.0f) {
276                                                         sc.weight = make_float3(0.0f, weight.y, 0.0f);
277                                                         sc.data0 = bssrdf->radius.y;
278                                                         sc.data1 = 0.0f;
279                                                         sd->flag |= bssrdf_setup(&sc, sc.type);
280                                                         sd->closure[sd->num_closure++] = sc;
281                                                 }
282
283                                                 if(fabsf(weight.z) > 0.0f) {
284                                                         sc.weight = make_float3(0.0f, 0.0f, weight.z);
285                                                         sc.data0 = bssrdf->radius.z;
286                                                         sc.data1 = 0.0f;
287                                                         sd->flag |= bssrdf_setup(&sc, sc.type);
288                                                         sd->closure[sd->num_closure++] = sc;
289                                                 }
290                                         }
291                                         break;
292                                 }
293                                 case CClosurePrimitive::Background:
294                                 case CClosurePrimitive::Volume:
295                                         break; /* not relevant */
296                         }
297                 }
298         }
299         else if(closure->type == OSL::ClosureColor::MUL) {
300                 OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
301                 flatten_surface_closure_tree(sd, path_flag, mul->closure, TO_FLOAT3(mul->weight) * weight);
302         }
303         else if(closure->type == OSL::ClosureColor::ADD) {
304                 OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
305                 flatten_surface_closure_tree(sd, path_flag, add->closureA, weight);
306                 flatten_surface_closure_tree(sd, path_flag, add->closureB, weight);
307         }
308 }
309
310 void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, int path_flag, ShaderContext ctx)
311 {
312         /* setup shader globals from shader data */
313         OSLThreadData *tdata = kg->osl_tdata;
314         shaderdata_to_shaderglobals(kg, sd, path_flag, tdata);
315
316         /* execute shader for this point */
317         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
318         OSL::ShaderGlobals *globals = &tdata->globals;
319         OSL::ShadingContext *octx = tdata->context[(int)ctx];
320         int shader = sd->shader & SHADER_MASK;
321
322         if(kg->osl->surface_state[shader])
323                 ss->execute(*octx, *(kg->osl->surface_state[shader]), *globals);
324
325         /* flatten closure tree */
326         if(globals->Ci)
327                 flatten_surface_closure_tree(sd, path_flag, globals->Ci);
328 }
329
330 /* Background */
331
332 static float3 flatten_background_closure_tree(const OSL::ClosureColor *closure)
333 {
334         /* OSL gives us a closure tree, if we are shading for background there
335          * is only one supported closure type at the moment, which has no evaluation
336          * functions, so we just sum the weights */
337
338         if(closure->type == OSL::ClosureColor::COMPONENT) {
339                 OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
340                 CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
341
342                 if(prim && prim->category == CClosurePrimitive::Background)
343 #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
344                         return TO_FLOAT3(comp->w);
345 #else
346                         return make_float3(1.0f, 1.0f, 1.0f);
347 #endif
348         }
349         else if(closure->type == OSL::ClosureColor::MUL) {
350                 OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
351
352                 return TO_FLOAT3(mul->weight) * flatten_background_closure_tree(mul->closure);
353         }
354         else if(closure->type == OSL::ClosureColor::ADD) {
355                 OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
356
357                 return flatten_background_closure_tree(add->closureA) +
358                        flatten_background_closure_tree(add->closureB);
359         }
360
361         return make_float3(0.0f, 0.0f, 0.0f);
362 }
363
364 float3 OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag, ShaderContext ctx)
365 {
366         /* setup shader globals from shader data */
367         OSLThreadData *tdata = kg->osl_tdata;
368         shaderdata_to_shaderglobals(kg, sd, path_flag, tdata);
369
370         /* execute shader for this point */
371         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
372         OSL::ShaderGlobals *globals = &tdata->globals;
373         OSL::ShadingContext *octx = tdata->context[(int)ctx];
374
375         if(kg->osl->background_state)
376                 ss->execute(*octx, *(kg->osl->background_state), *globals);
377
378         /* return background color immediately */
379         if(globals->Ci)
380                 return flatten_background_closure_tree(globals->Ci);
381
382         return make_float3(0.0f, 0.0f, 0.0f);
383 }
384
385 /* Volume */
386
387 static void flatten_volume_closure_tree(ShaderData *sd,
388                                         const OSL::ClosureColor *closure, float3 weight = make_float3(1.0f, 1.0f, 1.0f))
389 {
390         /* OSL gives us a closure tree, we flatten it into arrays per
391          * closure type, for evaluation, sampling, etc later on. */
392
393         if(closure->type == OSL::ClosureColor::COMPONENT) {
394                 OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
395                 CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
396
397                 if(prim) {
398                         ShaderClosure sc;
399
400 #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
401                         weight = weight*TO_FLOAT3(comp->w);
402 #endif
403                         sc.weight = weight;
404
405                         prim->setup();
406
407                         switch (prim->category) {
408                                 case CClosurePrimitive::Volume: {
409                                         CVolumeClosure *volume = (CVolumeClosure *)prim;
410                                         /* sample weight */
411                                         float sample_weight = fabsf(average(weight));
412
413                                         sc.sample_weight = sample_weight;
414                                         sc.type = volume->sc.type;
415                                         sc.data0 = volume->sc.data0;
416                                         sc.data1 = volume->sc.data1;
417
418                                         /* add */
419                                         if((sc.sample_weight > CLOSURE_WEIGHT_CUTOFF) &&
420                                            (sd->num_closure < MAX_CLOSURE))
421                                         {
422                                                 sd->closure[sd->num_closure++] = sc;
423                                                 sd->flag |= volume->shaderdata_flag();
424                                         }
425                                         break;
426                                 }
427                                 case CClosurePrimitive::Emissive: {
428                                         /* sample weight */
429                                         float sample_weight = fabsf(average(weight));
430
431                                         sc.sample_weight = sample_weight;
432                                         sc.type = CLOSURE_EMISSION_ID;
433                                         sc.data0 = 0.0f;
434                                         sc.data1 = 0.0f;
435                                         sc.prim = NULL;
436
437                                         /* flag */
438                                         if(sd->num_closure < MAX_CLOSURE) {
439                                                 sd->closure[sd->num_closure++] = sc;
440                                                 sd->flag |= SD_EMISSION;
441                                         }
442                                         break;
443                                 }
444                                 case CClosurePrimitive::Holdout:
445                                         break; /* not implemented */
446                                 case CClosurePrimitive::Background:
447                                 case CClosurePrimitive::BSDF:
448                                 case CClosurePrimitive::BSSRDF:
449                                 case CClosurePrimitive::AmbientOcclusion:
450                                         break; /* not relevant */
451                         }
452                 }
453         }
454         else if(closure->type == OSL::ClosureColor::MUL) {
455                 OSL::ClosureMul *mul = (OSL::ClosureMul *)closure;
456                 flatten_volume_closure_tree(sd, mul->closure, TO_FLOAT3(mul->weight) * weight);
457         }
458         else if(closure->type == OSL::ClosureColor::ADD) {
459                 OSL::ClosureAdd *add = (OSL::ClosureAdd *)closure;
460                 flatten_volume_closure_tree(sd, add->closureA, weight);
461                 flatten_volume_closure_tree(sd, add->closureB, weight);
462         }
463 }
464
465 void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, int path_flag, ShaderContext ctx)
466 {
467         /* setup shader globals from shader data */
468         OSLThreadData *tdata = kg->osl_tdata;
469         shaderdata_to_shaderglobals(kg, sd, path_flag, tdata);
470
471         /* execute shader */
472         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
473         OSL::ShaderGlobals *globals = &tdata->globals;
474         OSL::ShadingContext *octx = tdata->context[(int)ctx];
475         int shader = sd->shader & SHADER_MASK;
476
477         if(kg->osl->volume_state[shader])
478                 ss->execute(*octx, *(kg->osl->volume_state[shader]), *globals);
479         
480         /* flatten closure tree */
481         if(globals->Ci)
482                 flatten_volume_closure_tree(sd, globals->Ci);
483 }
484
485 /* Displacement */
486
487 void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd, ShaderContext ctx)
488 {
489         /* setup shader globals from shader data */
490         OSLThreadData *tdata = kg->osl_tdata;
491         shaderdata_to_shaderglobals(kg, sd, 0, tdata);
492
493         /* execute shader */
494         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)kg->osl_ss;
495         OSL::ShaderGlobals *globals = &tdata->globals;
496         OSL::ShadingContext *octx = tdata->context[(int)ctx];
497         int shader = sd->shader & SHADER_MASK;
498
499         if(kg->osl->displacement_state[shader])
500                 ss->execute(*octx, *(kg->osl->displacement_state[shader]), *globals);
501
502         /* get back position */
503         sd->P = TO_FLOAT3(globals->P);
504 }
505
506 /* BSDF Closure */
507
508 int OSLShader::bsdf_sample(const ShaderData *sd, const ShaderClosure *sc, float randu, float randv, float3& eval, float3& omega_in, differential3& domega_in, float& pdf)
509 {
510         CBSDFClosure *sample_bsdf = (CBSDFClosure *)sc->prim;
511
512         pdf = 0.0f;
513
514         return sample_bsdf->sample(sd->Ng,
515                                    sd->I, sd->dI.dx, sd->dI.dy,
516                                    randu, randv,
517                                    omega_in, domega_in.dx, domega_in.dy,
518                                    pdf, eval);
519 }
520
521 float3 OSLShader::bsdf_eval(const ShaderData *sd, const ShaderClosure *sc, const float3& omega_in, float& pdf)
522 {
523         CBSDFClosure *bsdf = (CBSDFClosure *)sc->prim;
524         float3 bsdf_eval;
525
526         if(dot(sd->Ng, omega_in) >= 0.0f)
527                 bsdf_eval = bsdf->eval_reflect(sd->I, omega_in, pdf);
528         else
529                 bsdf_eval = bsdf->eval_transmit(sd->I, omega_in, pdf);
530         
531         return bsdf_eval;
532 }
533
534 void OSLShader::bsdf_blur(ShaderClosure *sc, float roughness)
535 {
536         CBSDFClosure *bsdf = (CBSDFClosure *)sc->prim;
537         bsdf->blur(roughness);
538 }
539
540 /* Attributes */
541
542 int OSLShader::find_attribute(KernelGlobals *kg, const ShaderData *sd, uint id, AttributeElement *elem)
543 {
544         /* for OSL, a hash map is used to lookup the attribute by name. */
545         int object = sd->object*ATTR_PRIM_TYPES;
546 #ifdef __HAIR__
547         if(sd->type & PRIMITIVE_ALL_CURVE) object += ATTR_PRIM_CURVE;
548 #endif
549
550         OSLGlobals::AttributeMap &attr_map = kg->osl->attribute_map[object];
551         ustring stdname(std::string("geom:") + std::string(Attribute::standard_name((AttributeStandard)id)));
552         OSLGlobals::AttributeMap::const_iterator it = attr_map.find(stdname);
553
554         if(it != attr_map.end()) {
555                 const OSLGlobals::Attribute &osl_attr = it->second;
556                 *elem = osl_attr.elem;
557
558                 if(sd->prim == PRIM_NONE && (AttributeElement)osl_attr.elem != ATTR_ELEMENT_MESH)
559                         return ATTR_STD_NOT_FOUND;
560
561                 /* return result */
562                 return (osl_attr.elem == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND : osl_attr.offset;
563         }
564         else
565                 return (int)ATTR_STD_NOT_FOUND;
566 }
567
568 CCL_NAMESPACE_END
569