Cycles: Subsurface Scattering
[blender.git] / intern / cycles / render / osl.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "device.h"
20
21 #include "graph.h"
22 #include "light.h"
23 #include "osl.h"
24 #include "scene.h"
25 #include "shader.h"
26
27 #ifdef WITH_OSL
28
29 #include "osl_globals.h"
30 #include "osl_services.h"
31 #include "osl_shader.h"
32
33 #include "util_foreach.h"
34 #include "util_md5.h"
35 #include "util_path.h"
36 #include "util_progress.h"
37
38 #endif
39
40 CCL_NAMESPACE_BEGIN
41
42 #ifdef WITH_OSL
43
44 /* Shared Texture and Shading System */
45
46 OSL::TextureSystem *OSLShaderManager::ts_shared = NULL;
47 int OSLShaderManager::ts_shared_users = 0;
48 thread_mutex OSLShaderManager::ts_shared_mutex;
49
50 OSL::ShadingSystem *OSLShaderManager::ss_shared = NULL;
51 OSLRenderServices *OSLShaderManager::services_shared = NULL;
52 int OSLShaderManager::ss_shared_users = 0;
53 thread_mutex OSLShaderManager::ss_shared_mutex;
54 thread_mutex OSLShaderManager::ss_mutex;
55
56 /* Shader Manager */
57
58 OSLShaderManager::OSLShaderManager()
59 {
60         texture_system_init();
61         shading_system_init();
62 }
63
64 OSLShaderManager::~OSLShaderManager()
65 {
66         shading_system_free();
67         texture_system_free();
68 }
69
70 void OSLShaderManager::reset(Scene *scene)
71 {
72         shading_system_free();
73         shading_system_init();
74 }
75
76 void OSLShaderManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
77 {
78         if(!need_update)
79                 return;
80
81         device_free(device, dscene, scene);
82
83         /* determine which shaders are in use */
84         device_update_shaders_used(scene);
85
86         /* create shaders */
87         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
88
89         foreach(Shader *shader, scene->shaders) {
90                 assert(shader->graph);
91
92                 if(progress.get_cancel()) return;
93
94                 /* we can only compile one shader at the time as the OSL ShadingSytem
95                  * has a single state, but we put the lock here so different renders can
96                  * compile shaders alternating */
97                 thread_scoped_lock lock(ss_mutex);
98
99                 OSLCompiler compiler((void*)this, (void*)ss, scene->image_manager);
100                 compiler.background = (shader == scene->shaders[scene->default_background]);
101                 compiler.compile(og, shader);
102
103                 if(shader->use_mis && shader->has_surface_emission)
104                         scene->light_manager->need_update = true;
105         }
106
107         /* setup shader engine */
108         og->ss = ss;
109         og->ts = ts;
110         og->services = services;
111
112         int background_id = scene->shader_manager->get_shader_id(scene->default_background);
113         og->background_state = og->surface_state[background_id & SHADER_MASK];
114         og->use = true;
115
116         foreach(Shader *shader, scene->shaders)
117                 shader->need_update = false;
118
119         need_update = false;
120         
121         /* set texture system */
122         scene->image_manager->set_osl_texture_system((void*)ts);
123
124         device_update_common(device, dscene, scene, progress);
125
126         /* greedyjit test
127         {
128                 thread_scoped_lock lock(ss_shared_mutex);
129                 ss->optimize_all_groups();
130         }*/
131 }
132
133 void OSLShaderManager::device_free(Device *device, DeviceScene *dscene, Scene *scene)
134 {
135         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
136
137         device_free_common(device, dscene, scene);
138
139         /* clear shader engine */
140         og->use = false;
141         og->ss = NULL;
142         og->ts = NULL;
143
144         og->surface_state.clear();
145         og->volume_state.clear();
146         og->displacement_state.clear();
147         og->background_state.reset();
148 }
149
150 void OSLShaderManager::texture_system_init()
151 {
152         /* create texture system, shared between different renders to reduce memory usage */
153         thread_scoped_lock lock(ts_shared_mutex);
154
155         if(ts_shared_users == 0) {
156                 ts_shared = TextureSystem::create(true);
157
158                 ts_shared->attribute("automip",  1);
159                 ts_shared->attribute("autotile", 64);
160                 ts_shared->attribute("gray_to_rgb", 1);
161
162                 /* effectively unlimited for now, until we support proper mipmap lookups */
163                 ts_shared->attribute("max_memory_MB", 16384);
164         }
165
166         ts = ts_shared;
167         ts_shared_users++;
168 }
169
170 void OSLShaderManager::texture_system_free()
171 {
172         /* shared texture system decrease users and destroy if no longer used */
173         thread_scoped_lock lock(ts_shared_mutex);
174         ts_shared_users--;
175
176         if(ts_shared_users == 0) {
177                 OSL::TextureSystem::destroy(ts_shared);
178                 ts_shared = NULL;
179         }
180
181         ts = NULL;
182 }
183
184 void OSLShaderManager::shading_system_init()
185 {
186         /* create shading system, shared between different renders to reduce memory usage */
187         thread_scoped_lock lock(ss_shared_mutex);
188
189         if(ss_shared_users == 0) {
190                 services_shared = new OSLRenderServices();
191
192                 ss_shared = OSL::ShadingSystem::create(services_shared, ts_shared, &errhandler);
193                 ss_shared->attribute("lockgeom", 1);
194                 ss_shared->attribute("commonspace", "world");
195                 ss_shared->attribute("searchpath:shader", path_get("shader"));
196                 //ss_shared->attribute("greedyjit", 1);
197
198                 /* our own ray types */
199                 static const char *raytypes[] = {
200                         "camera",               /* PATH_RAY_CAMERA */
201                         "reflection",   /* PATH_RAY_REFLECT */
202                         "refraction",   /* PATH_RAY_TRANSMIT */
203                         "diffuse",              /* PATH_RAY_DIFFUSE */
204                         "glossy",               /* PATH_RAY_GLOSSY */
205                         "singular",             /* PATH_RAY_SINGULAR */
206                         "transparent",  /* PATH_RAY_TRANSPARENT */
207                         "shadow",               /* PATH_RAY_SHADOW_OPAQUE */
208                         "shadow",               /* PATH_RAY_SHADOW_TRANSPARENT */
209
210                         "__unused__",
211                         "__unused__",
212                         "diffuse_ancestor", /* PATH_RAY_DIFFUSE_ANCESTOR */
213                         "glossy_ancestor",  /* PATH_RAY_GLOSSY_ANCESTOR */
214                 };
215
216                 const int nraytypes = sizeof(raytypes)/sizeof(raytypes[0]);
217                 ss_shared->attribute("raytypes", TypeDesc(TypeDesc::STRING, nraytypes), raytypes);
218
219                 OSLShader::register_closures((OSLShadingSystem*)ss_shared);
220
221                 loaded_shaders.clear();
222         }
223
224         ss = ss_shared;
225         services = services_shared;
226         ss_shared_users++;
227 }
228
229 void OSLShaderManager::shading_system_free()
230 {
231         /* shared shading system decrease users and destroy if no longer used */
232         thread_scoped_lock lock(ss_shared_mutex);
233         ss_shared_users--;
234
235         if(ss_shared_users == 0) {
236                 OSL::ShadingSystem::destroy(ss_shared);
237                 ss_shared = NULL;
238
239                 delete services_shared;
240                 services_shared = NULL;
241         }
242
243         ss = NULL;
244         services = NULL;
245 }
246
247 bool OSLShaderManager::osl_compile(const string& inputfile, const string& outputfile)
248 {
249         vector<string> options;
250         string stdosl_path;
251
252         /* specify output file name */
253         options.push_back("-o");
254         options.push_back(outputfile);
255
256         /* specify standard include path */
257         options.push_back("-I" + path_get("shader"));
258         stdosl_path = path_get("shader/stdosl.h");
259
260         /* compile */
261         OSL::OSLCompiler *compiler = OSL::OSLCompiler::create();
262         bool ok = compiler->compile(inputfile, options, stdosl_path);
263         delete compiler;
264
265         return ok;
266 }
267
268 bool OSLShaderManager::osl_query(OSL::OSLQuery& query, const string& filepath)
269 {
270         string searchpath = path_user_get("shaders");
271         return query.open(filepath, searchpath);
272 }
273
274 static string shader_filepath_hash(const string& filepath, uint64_t modified_time)
275 {
276         /* compute a hash from filepath and modified time to detect changes */
277         MD5Hash md5;
278         md5.append((const uint8_t*)filepath.c_str(), filepath.size());
279         md5.append((const uint8_t*)&modified_time, sizeof(modified_time));
280
281         return md5.get_hex();
282 }
283
284 const char *OSLShaderManager::shader_test_loaded(const string& hash)
285 {
286         map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
287         return (it == loaded_shaders.end())? NULL: it->first.c_str();
288 }
289
290 OSLShaderInfo *OSLShaderManager::shader_loaded_info(const string& hash)
291 {
292         map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
293         return (it == loaded_shaders.end())? NULL: &it->second;
294 }
295
296 const char *OSLShaderManager::shader_load_filepath(string filepath)
297 {
298         size_t len = filepath.size();
299         string extension = filepath.substr(len - 4);
300         uint64_t modified_time = path_modified_time(filepath);
301
302         if(extension == ".osl") {
303                 /* .OSL File */
304                 string osopath = filepath.substr(0, len - 4) + ".oso";
305                 uint64_t oso_modified_time = path_modified_time(osopath);
306
307                 /* test if we have loaded the corresponding .OSO already */
308                 if(oso_modified_time != 0) {
309                         const char *hash = shader_test_loaded(shader_filepath_hash(osopath, oso_modified_time));
310
311                         if(hash)
312                                 return hash;
313                 }
314
315                 /* autocompile .OSL to .OSO if needed */
316                 if(oso_modified_time == 0 || (oso_modified_time < modified_time)) {
317                         OSLShaderManager::osl_compile(filepath, osopath);
318                         modified_time = path_modified_time(osopath);
319                 }
320                 else
321                         modified_time = oso_modified_time;
322
323                 filepath = osopath;
324         }
325         else {
326                 if(extension == ".oso") {
327                         /* .OSO File, nothing to do */
328                 }
329                 else if(path_dirname(filepath) == "") {
330                         /* .OSO File in search path */
331                         filepath = path_join(path_user_get("shaders"), filepath + ".oso");
332                 }
333                 else {
334                         /* unknown file */
335                         return NULL;
336                 }
337
338                 /* test if we have loaded this .OSO already */
339                 const char *hash = shader_test_loaded(shader_filepath_hash(filepath, modified_time));
340
341                 if(hash)
342                         return hash;
343         }
344
345         /* read oso bytecode from file */
346         string bytecode_hash = shader_filepath_hash(filepath, modified_time);
347         string bytecode;
348
349         if(!path_read_text(filepath, bytecode)) {
350                 fprintf(stderr, "Cycles shader graph: failed to read file %s\n", filepath.c_str());
351                 OSLShaderInfo info;
352                 loaded_shaders[bytecode_hash] = info; /* to avoid repeat tries */
353                 return NULL;
354         }
355
356         return shader_load_bytecode(bytecode_hash, bytecode);
357 }
358
359 const char *OSLShaderManager::shader_load_bytecode(const string& hash, const string& bytecode)
360 {
361         ss->LoadMemoryCompiledShader(hash.c_str(), bytecode.c_str());
362
363         /* this is a bit weak, but works */
364         OSLShaderInfo info;
365         info.has_surface_emission = (bytecode.find("\"emission\"") != string::npos);
366         info.has_surface_transparent = (bytecode.find("\"transparent\"") != string::npos);
367         info.has_surface_bssrdf = (bytecode.find("\"bssrdf\"") != string::npos);
368         loaded_shaders[hash] = info;
369
370         return loaded_shaders.find(hash)->first.c_str();
371 }
372
373 /* Graph Compiler */
374
375 OSLCompiler::OSLCompiler(void *manager_, void *shadingsys_, ImageManager *image_manager_)
376 {
377         manager = manager_;
378         shadingsys = shadingsys_;
379         image_manager = image_manager_;
380         current_type = SHADER_TYPE_SURFACE;
381         current_shader = NULL;
382         background = false;
383 }
384
385 string OSLCompiler::id(ShaderNode *node)
386 {
387         /* assign layer unique name based on pointer address + bump mode */
388         stringstream stream;
389         stream << "node_" << node->name << "_" << node;
390
391         return stream.str();
392 }
393
394 string OSLCompiler::compatible_name(ShaderNode *node, ShaderInput *input)
395 {
396         string sname(input->name);
397         size_t i;
398
399         /* strip whitespace */
400         while((i = sname.find(" ")) != string::npos)
401                 sname.replace(i, 1, "");
402         
403         /* if output exists with the same name, add "In" suffix */
404         foreach(ShaderOutput *output, node->outputs) {
405                 if (strcmp(input->name, output->name)==0) {
406                         sname += "In";
407                         break;
408                 }
409         }
410         
411         return sname;
412 }
413
414 string OSLCompiler::compatible_name(ShaderNode *node, ShaderOutput *output)
415 {
416         string sname(output->name);
417         size_t i;
418
419         /* strip whitespace */
420         while((i = sname.find(" ")) != string::npos)
421                 sname.replace(i, 1, "");
422         
423         /* if input exists with the same name, add "Out" suffix */
424         foreach(ShaderInput *input, node->inputs) {
425                 if (strcmp(input->name, output->name)==0) {
426                         sname += "Out";
427                         break;
428                 }
429         }
430         
431         return sname;
432 }
433
434 bool OSLCompiler::node_skip_input(ShaderNode *node, ShaderInput *input)
435 {
436         /* exception for output node, only one input is actually used
437          * depending on the current shader type */
438         
439         if(!(input->usage & ShaderInput::USE_OSL))
440                 return true;
441
442         if(node->name == ustring("output")) {
443                 if(strcmp(input->name, "Surface") == 0 && current_type != SHADER_TYPE_SURFACE)
444                         return true;
445                 if(strcmp(input->name, "Volume") == 0 && current_type != SHADER_TYPE_VOLUME)
446                         return true;
447                 if(strcmp(input->name, "Displacement") == 0 && current_type != SHADER_TYPE_DISPLACEMENT)
448                         return true;
449                 if(strcmp(input->name, "Normal") == 0)
450                         return true;
451         }
452         else if(node->name == ustring("bump")) {
453                 if(strcmp(input->name, "Height") == 0)
454                         return true;
455         }
456         else if(current_type == SHADER_TYPE_DISPLACEMENT && input->link && input->link->parent->name == ustring("bump"))
457                 return true;
458
459         return false;
460 }
461
462 void OSLCompiler::add(ShaderNode *node, const char *name, bool isfilepath)
463 {
464         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
465
466         /* load filepath */
467         if(isfilepath) {
468                 name = ((OSLShaderManager*)manager)->shader_load_filepath(name);
469
470                 if(name == NULL)
471                         return;
472         }
473
474         /* pass in fixed parameter values */
475         foreach(ShaderInput *input, node->inputs) {
476                 if(!input->link) {
477                         /* checks to untangle graphs */
478                         if(node_skip_input(node, input))
479                                 continue;
480                         /* already has default value assigned */
481                         else if(input->default_value != ShaderInput::NONE)
482                                 continue;
483
484                         string param_name = compatible_name(node, input);
485                         switch(input->type) {
486                                 case SHADER_SOCKET_COLOR:
487                                         parameter_color(param_name.c_str(), input->value);
488                                         break;
489                                 case SHADER_SOCKET_POINT:
490                                         parameter_point(param_name.c_str(), input->value);
491                                         break;
492                                 case SHADER_SOCKET_VECTOR:
493                                         parameter_vector(param_name.c_str(), input->value);
494                                         break;
495                                 case SHADER_SOCKET_NORMAL:
496                                         parameter_normal(param_name.c_str(), input->value);
497                                         break;
498                                 case SHADER_SOCKET_FLOAT:
499                                         parameter(param_name.c_str(), input->value.x);
500                                         break;
501                                 case SHADER_SOCKET_INT:
502                                         parameter(param_name.c_str(), (int)input->value.x);
503                                         break;
504                                 case SHADER_SOCKET_STRING:
505                                         parameter(param_name.c_str(), input->value_string);
506                                         break;
507                                 case SHADER_SOCKET_CLOSURE:
508                                 case SHADER_SOCKET_UNDEFINED:
509                                         break;
510                         }
511                 }
512         }
513
514         /* create shader of the appropriate type. we pass "surface" to all shaders,
515          * because "volume" and "displacement" don't work yet in OSL. the shaders
516          * work fine, but presumably these values would be used for more strict
517          * checking, so when that is fixed, we should update the code here too. */
518         if(current_type == SHADER_TYPE_SURFACE)
519                 ss->Shader("surface", name, id(node).c_str());
520         else if(current_type == SHADER_TYPE_VOLUME)
521                 ss->Shader("surface", name, id(node).c_str());
522         else if(current_type == SHADER_TYPE_DISPLACEMENT)
523                 ss->Shader("surface", name, id(node).c_str());
524         else
525                 assert(0);
526         
527         /* link inputs to other nodes */
528         foreach(ShaderInput *input, node->inputs) {
529                 if(input->link) {
530                         if(node_skip_input(node, input))
531                                 continue;
532
533                         /* connect shaders */
534                         string id_from = id(input->link->parent);
535                         string id_to = id(node);
536                         string param_from = compatible_name(input->link->parent, input->link);
537                         string param_to = compatible_name(node, input);
538
539                         ss->ConnectShaders(id_from.c_str(), param_from.c_str(), id_to.c_str(), param_to.c_str());
540                 }
541         }
542
543         /* test if we shader contains specific closures */
544         OSLShaderInfo *info = ((OSLShaderManager*)manager)->shader_loaded_info(name);
545
546         if(info) {
547                 if(info->has_surface_emission)
548                         current_shader->has_surface_emission = true;
549                 if(info->has_surface_transparent)
550                         current_shader->has_surface_transparent = true;
551                 if(info->has_surface_bssrdf) {
552                         current_shader->has_surface_bssrdf = true;
553                         current_shader->has_bssrdf_bump = true; /* can't detect yet */
554                 }
555         }
556 }
557
558 void OSLCompiler::parameter(const char *name, float f)
559 {
560         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
561         ss->Parameter(name, TypeDesc::TypeFloat, &f);
562 }
563
564 void OSLCompiler::parameter_color(const char *name, float3 f)
565 {
566         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
567         ss->Parameter(name, TypeDesc::TypeColor, &f);
568 }
569
570 void OSLCompiler::parameter_point(const char *name, float3 f)
571 {
572         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
573         ss->Parameter(name, TypeDesc::TypePoint, &f);
574 }
575
576 void OSLCompiler::parameter_normal(const char *name, float3 f)
577 {
578         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
579         ss->Parameter(name, TypeDesc::TypeNormal, &f);
580 }
581
582 void OSLCompiler::parameter_vector(const char *name, float3 f)
583 {
584         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
585         ss->Parameter(name, TypeDesc::TypeVector, &f);
586 }
587
588 void OSLCompiler::parameter(const char *name, int f)
589 {
590         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
591         ss->Parameter(name, TypeDesc::TypeInt, &f);
592 }
593
594 void OSLCompiler::parameter(const char *name, const char *s)
595 {
596         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
597         ss->Parameter(name, TypeDesc::TypeString, &s);
598 }
599
600 void OSLCompiler::parameter(const char *name, ustring s)
601 {
602         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
603         const char *str = s.c_str();
604         ss->Parameter(name, TypeDesc::TypeString, &str);
605 }
606
607 void OSLCompiler::parameter(const char *name, const Transform& tfm)
608 {
609         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
610         ss->Parameter(name, TypeDesc::TypeMatrix, (float*)&tfm);
611 }
612
613 void OSLCompiler::parameter_array(const char *name, const float f[], int arraylen)
614 {
615         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
616         TypeDesc type = TypeDesc::TypeFloat;
617         type.arraylen = arraylen;
618         ss->Parameter(name, type, f);
619 }
620
621 void OSLCompiler::parameter_color_array(const char *name, const float f[][3], int arraylen)
622 {
623         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
624         TypeDesc type = TypeDesc::TypeColor;
625         type.arraylen = arraylen;
626         ss->Parameter(name, type, f);
627 }
628
629 void OSLCompiler::parameter_vector_array(const char *name, const float f[][3], int arraylen)
630 {
631         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
632         TypeDesc type = TypeDesc::TypeVector;
633         type.arraylen = arraylen;
634         ss->Parameter(name, type, f);
635 }
636
637 void OSLCompiler::parameter_normal_array(const char *name, const float f[][3], int arraylen)
638 {
639         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
640         TypeDesc type = TypeDesc::TypeNormal;
641         type.arraylen = arraylen;
642         ss->Parameter(name, type, f);
643 }
644
645 void OSLCompiler::parameter_point_array(const char *name, const float f[][3], int arraylen)
646 {
647         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
648         TypeDesc type = TypeDesc::TypePoint;
649         type.arraylen = arraylen;
650         ss->Parameter(name, type, f);
651 }
652
653 void OSLCompiler::parameter_array(const char *name, const int f[], int arraylen)
654 {
655         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
656         TypeDesc type = TypeDesc::TypeInt;
657         type.arraylen = arraylen;
658         ss->Parameter(name, type, f);
659 }
660
661 void OSLCompiler::parameter_array(const char *name, const char * const s[], int arraylen)
662 {
663         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
664         TypeDesc type = TypeDesc::TypeString;
665         type.arraylen = arraylen;
666         ss->Parameter(name, type, s);
667 }
668
669 void OSLCompiler::parameter_array(const char *name, const Transform tfm[], int arraylen)
670 {
671         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
672         TypeDesc type = TypeDesc::TypeMatrix;
673         type.arraylen = arraylen;
674         ss->Parameter(name, type, (const float *)tfm);
675 }
676
677 void OSLCompiler::find_dependencies(set<ShaderNode*>& dependencies, ShaderInput *input)
678 {
679         ShaderNode *node = (input->link)? input->link->parent: NULL;
680
681         if(node) {
682                 foreach(ShaderInput *in, node->inputs)
683                         if(!node_skip_input(node, in))
684                                 find_dependencies(dependencies, in);
685
686                 dependencies.insert(node);
687         }
688 }
689
690 void OSLCompiler::generate_nodes(const set<ShaderNode*>& nodes)
691 {
692         set<ShaderNode*> done;
693         bool nodes_done;
694
695         do {
696                 nodes_done = true;
697
698                 foreach(ShaderNode *node, nodes) {
699                         if(done.find(node) == done.end()) {
700                                 bool inputs_done = true;
701
702                                 foreach(ShaderInput *input, node->inputs)
703                                         if(!node_skip_input(node, input))
704                                                 if(input->link && done.find(input->link->parent) == done.end())
705                                                         inputs_done = false;
706
707                                 if(inputs_done) {
708                                         node->compile(*this);
709                                         done.insert(node);
710
711                                         if(node->has_surface_emission())
712                                                 current_shader->has_surface_emission = true;
713                                         if(node->has_surface_transparent())
714                                                 current_shader->has_surface_transparent = true;
715                                         if(node->has_surface_bssrdf()) {
716                                                 current_shader->has_surface_bssrdf = true;
717                                                 if(node->has_bssrdf_bump())
718                                                         current_shader->has_bssrdf_bump = true;
719                                         }
720                                 }
721                                 else
722                                         nodes_done = false;
723                         }
724                 }
725         } while(!nodes_done);
726 }
727
728 void OSLCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
729 {
730         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
731
732         current_type = type;
733
734         ss->ShaderGroupBegin(shader->name.c_str());
735
736         ShaderNode *output = graph->output();
737         set<ShaderNode*> dependencies;
738
739         if(type == SHADER_TYPE_SURFACE) {
740                 /* generate surface shader */
741                 find_dependencies(dependencies, output->input("Surface"));
742                 generate_nodes(dependencies);
743                 output->compile(*this);
744         }
745         else if(type == SHADER_TYPE_VOLUME) {
746                 /* generate volume shader */
747                 find_dependencies(dependencies, output->input("Volume"));
748                 generate_nodes(dependencies);
749                 output->compile(*this);
750         }
751         else if(type == SHADER_TYPE_DISPLACEMENT) {
752                 /* generate displacement shader */
753                 find_dependencies(dependencies, output->input("Displacement"));
754                 generate_nodes(dependencies);
755                 output->compile(*this);
756         }
757         else
758                 assert(0);
759
760         ss->ShaderGroupEnd();
761 }
762
763 void OSLCompiler::compile(OSLGlobals *og, Shader *shader)
764 {
765         if(shader->need_update) {
766                 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
767                 ShaderGraph *graph = shader->graph;
768                 ShaderNode *output = (graph)? graph->output(): NULL;
769
770                 /* copy graph for shader with bump mapping */
771                 if(output->input("Surface")->link && output->input("Displacement")->link)
772                         if(!shader->graph_bump)
773                                 shader->graph_bump = shader->graph->copy();
774
775                 /* finalize */
776                 shader->graph->finalize(false, true);
777                 if(shader->graph_bump)
778                         shader->graph_bump->finalize(true, true);
779
780                 current_shader = shader;
781
782                 shader->has_surface = false;
783                 shader->has_surface_emission = false;
784                 shader->has_surface_transparent = false;
785                 shader->has_surface_bssrdf = false;
786                 shader->has_bssrdf_bump = false;
787                 shader->has_volume = false;
788                 shader->has_displacement = false;
789
790                 /* generate surface shader */
791                 if(shader->used && graph && output->input("Surface")->link) {
792                         compile_type(shader, shader->graph, SHADER_TYPE_SURFACE);
793                         shader->osl_surface_ref = ss->state();
794
795                         if(shader->graph_bump) {
796                                 ss->clear_state();
797                                 compile_type(shader, shader->graph_bump, SHADER_TYPE_SURFACE);
798                                 shader->osl_surface_bump_ref = ss->state();
799                         }
800                         else
801                                 shader->osl_surface_bump_ref = shader->osl_surface_ref;
802
803                         ss->clear_state();
804
805                         shader->has_surface = true;
806                 }
807                 else {
808                         shader->osl_surface_ref = OSL::ShadingAttribStateRef();
809                         shader->osl_surface_bump_ref = OSL::ShadingAttribStateRef();
810                 }
811
812                 /* generate volume shader */
813                 if(shader->used && graph && output->input("Volume")->link) {
814                         compile_type(shader, shader->graph, SHADER_TYPE_VOLUME);
815                         shader->has_volume = true;
816
817                         shader->osl_volume_ref = ss->state();
818                         ss->clear_state();
819                 }
820                 else
821                         shader->osl_volume_ref = OSL::ShadingAttribStateRef();
822
823                 /* generate displacement shader */
824                 if(shader->used && graph && output->input("Displacement")->link) {
825                         compile_type(shader, shader->graph, SHADER_TYPE_DISPLACEMENT);
826                         shader->has_displacement = true;
827                         shader->osl_displacement_ref = ss->state();
828                         ss->clear_state();
829                 }
830                 else
831                         shader->osl_displacement_ref = OSL::ShadingAttribStateRef();
832         }
833
834         /* push state to array for lookup */
835         og->surface_state.push_back(shader->osl_surface_ref);
836         og->surface_state.push_back(shader->osl_surface_bump_ref);
837
838         og->volume_state.push_back(shader->osl_volume_ref);
839         og->volume_state.push_back(shader->osl_volume_ref);
840
841         og->displacement_state.push_back(shader->osl_displacement_ref);
842         og->displacement_state.push_back(shader->osl_displacement_ref);
843 }
844
845 #else
846
847 void OSLCompiler::add(ShaderNode *node, const char *name, bool isfilepath)
848 {
849 }
850
851 void OSLCompiler::parameter(const char *name, float f)
852 {
853 }
854
855 void OSLCompiler::parameter_color(const char *name, float3 f)
856 {
857 }
858
859 void OSLCompiler::parameter_vector(const char *name, float3 f)
860 {
861 }
862
863 void OSLCompiler::parameter_point(const char *name, float3 f)
864 {
865 }
866
867 void OSLCompiler::parameter_normal(const char *name, float3 f)
868 {
869 }
870
871 void OSLCompiler::parameter(const char *name, int f)
872 {
873 }
874
875 void OSLCompiler::parameter(const char *name, const char *s)
876 {
877 }
878
879 void OSLCompiler::parameter(const char *name, ustring s)
880 {
881 }
882
883 void OSLCompiler::parameter(const char *name, const Transform& tfm)
884 {
885 }
886
887 void OSLCompiler::parameter_array(const char *name, const float f[], int arraylen)
888 {
889 }
890
891 void OSLCompiler::parameter_color_array(const char *name, const float f[][3], int arraylen)
892 {
893 }
894
895 void OSLCompiler::parameter_vector_array(const char *name, const float f[][3], int arraylen)
896 {
897 }
898
899 void OSLCompiler::parameter_normal_array(const char *name, const float f[][3], int arraylen)
900 {
901 }
902
903 void OSLCompiler::parameter_point_array(const char *name, const float f[][3], int arraylen)
904 {
905 }
906
907 void OSLCompiler::parameter_array(const char *name, const int f[], int arraylen)
908 {
909 }
910
911 void OSLCompiler::parameter_array(const char *name, const char * const s[], int arraylen)
912 {
913 }
914
915 void OSLCompiler::parameter_array(const char *name, const Transform tfm[], int arraylen)
916 {
917 }
918
919 #endif /* WITH_OSL */
920
921 CCL_NAMESPACE_END
922