3f269f44abe4f19278c1b8c0d320aae932485b0e
[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                         "gloss_sharedy",                /* 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
211                 const int nraytypes = sizeof(raytypes)/sizeof(raytypes[0]);
212                 ss_shared->attribute("raytypes", TypeDesc(TypeDesc::STRING, nraytypes), raytypes);
213
214                 OSLShader::register_closures((OSLShadingSystem*)ss_shared);
215
216                 loaded_shaders.clear();
217         }
218
219         ss = ss_shared;
220         services = services_shared;
221         ss_shared_users++;
222 }
223
224 void OSLShaderManager::shading_system_free()
225 {
226         /* shared shading system decrease users and destroy if no longer used */
227         thread_scoped_lock lock(ss_shared_mutex);
228         ss_shared_users--;
229
230         if(ss_shared_users == 0) {
231                 OSL::ShadingSystem::destroy(ss_shared);
232                 ss_shared = NULL;
233
234                 delete services_shared;
235                 services_shared = NULL;
236         }
237
238         ss = NULL;
239         services = NULL;
240 }
241
242 bool OSLShaderManager::osl_compile(const string& inputfile, const string& outputfile)
243 {
244         vector<string> options;
245         string stdosl_path;
246
247         /* specify output file name */
248         options.push_back("-o");
249         options.push_back(outputfile);
250
251         /* specify standard include path */
252         options.push_back("-I" + path_get("shader"));
253         stdosl_path = path_get("shader/stdosl.h");
254
255         /* compile */
256         OSL::OSLCompiler *compiler = OSL::OSLCompiler::create();
257         bool ok = compiler->compile(inputfile, options, stdosl_path);
258         delete compiler;
259
260         return ok;
261 }
262
263 bool OSLShaderManager::osl_query(OSL::OSLQuery& query, const string& filepath)
264 {
265         string searchpath = path_user_get("shaders");
266         return query.open(filepath, searchpath);
267 }
268
269 static string shader_filepath_hash(const string& filepath, uint64_t modified_time)
270 {
271         /* compute a hash from filepath and modified time to detect changes */
272         MD5Hash md5;
273         md5.append((const uint8_t*)filepath.c_str(), filepath.size());
274         md5.append((const uint8_t*)&modified_time, sizeof(modified_time));
275
276         return md5.get_hex();
277 }
278
279 const char *OSLShaderManager::shader_test_loaded(const string& hash)
280 {
281         map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
282         return (it == loaded_shaders.end())? NULL: it->first.c_str();
283 }
284
285 OSLShaderInfo *OSLShaderManager::shader_loaded_info(const string& hash)
286 {
287         map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
288         return (it == loaded_shaders.end())? NULL: &it->second;
289 }
290
291 const char *OSLShaderManager::shader_load_filepath(string filepath)
292 {
293         size_t len = filepath.size();
294         string extension = filepath.substr(len - 4);
295         uint64_t modified_time = path_modified_time(filepath);
296
297         if(extension == ".osl") {
298                 /* .OSL File */
299                 string osopath = filepath.substr(0, len - 4) + ".oso";
300                 uint64_t oso_modified_time = path_modified_time(osopath);
301
302                 /* test if we have loaded the corresponding .OSO already */
303                 if(oso_modified_time != 0) {
304                         const char *hash = shader_test_loaded(shader_filepath_hash(osopath, oso_modified_time));
305
306                         if(hash)
307                                 return hash;
308                 }
309
310                 /* autocompile .OSL to .OSO if needed */
311                 if(oso_modified_time == 0 || (oso_modified_time < modified_time)) {
312                         OSLShaderManager::osl_compile(filepath, osopath);
313                         modified_time = path_modified_time(osopath);
314                 }
315                 else
316                         modified_time = oso_modified_time;
317
318                 filepath = osopath;
319         }
320         else {
321                 if(extension == ".oso") {
322                         /* .OSO File, nothing to do */
323                 }
324                 else if(path_dirname(filepath) == "") {
325                         /* .OSO File in search path */
326                         filepath = path_join(path_user_get("shaders"), filepath + ".oso");
327                 }
328                 else {
329                         /* unknown file */
330                         return NULL;
331                 }
332
333                 /* test if we have loaded this .OSO already */
334                 const char *hash = shader_test_loaded(shader_filepath_hash(filepath, modified_time));
335
336                 if(hash)
337                         return hash;
338         }
339
340         /* read oso bytecode from file */
341         string bytecode_hash = shader_filepath_hash(filepath, modified_time);
342         string bytecode;
343
344         if(!path_read_text(filepath, bytecode)) {
345                 fprintf(stderr, "Cycles shader graph: failed to read file %s\n", filepath.c_str());
346                 OSLShaderInfo info;
347                 loaded_shaders[bytecode_hash] = info; /* to avoid repeat tries */
348                 return NULL;
349         }
350
351         return shader_load_bytecode(bytecode_hash, bytecode);
352 }
353
354 const char *OSLShaderManager::shader_load_bytecode(const string& hash, const string& bytecode)
355 {
356         ss->LoadMemoryCompiledShader(hash.c_str(), bytecode.c_str());
357
358         /* this is a bit weak, but works */
359         OSLShaderInfo info;
360         info.has_surface_emission = (bytecode.find("\"emission\"") != string::npos);
361         info.has_surface_transparent = (bytecode.find("\"transparent\"") != string::npos);
362         info.has_surface_bssrdf = (bytecode.find("\"bssrdf\"") != string::npos);
363         loaded_shaders[hash] = info;
364
365         return loaded_shaders.find(hash)->first.c_str();
366 }
367
368 /* Graph Compiler */
369
370 OSLCompiler::OSLCompiler(void *manager_, void *shadingsys_, ImageManager *image_manager_)
371 {
372         manager = manager_;
373         shadingsys = shadingsys_;
374         image_manager = image_manager_;
375         current_type = SHADER_TYPE_SURFACE;
376         current_shader = NULL;
377         background = false;
378 }
379
380 string OSLCompiler::id(ShaderNode *node)
381 {
382         /* assign layer unique name based on pointer address + bump mode */
383         stringstream stream;
384         stream << "node_" << node->name << "_" << node;
385
386         return stream.str();
387 }
388
389 string OSLCompiler::compatible_name(ShaderNode *node, ShaderInput *input)
390 {
391         string sname(input->name);
392         size_t i;
393
394         /* strip whitespace */
395         while((i = sname.find(" ")) != string::npos)
396                 sname.replace(i, 1, "");
397         
398         /* if output exists with the same name, add "In" suffix */
399         foreach(ShaderOutput *output, node->outputs) {
400                 if (strcmp(input->name, output->name)==0) {
401                         sname += "In";
402                         break;
403                 }
404         }
405         
406         return sname;
407 }
408
409 string OSLCompiler::compatible_name(ShaderNode *node, ShaderOutput *output)
410 {
411         string sname(output->name);
412         size_t i;
413
414         /* strip whitespace */
415         while((i = sname.find(" ")) != string::npos)
416                 sname.replace(i, 1, "");
417         
418         /* if input exists with the same name, add "Out" suffix */
419         foreach(ShaderInput *input, node->inputs) {
420                 if (strcmp(input->name, output->name)==0) {
421                         sname += "Out";
422                         break;
423                 }
424         }
425         
426         return sname;
427 }
428
429 bool OSLCompiler::node_skip_input(ShaderNode *node, ShaderInput *input)
430 {
431         /* exception for output node, only one input is actually used
432          * depending on the current shader type */
433         
434         if(!(input->usage & ShaderInput::USE_OSL))
435                 return true;
436
437         if(node->name == ustring("output")) {
438                 if(strcmp(input->name, "Surface") == 0 && current_type != SHADER_TYPE_SURFACE)
439                         return true;
440                 if(strcmp(input->name, "Volume") == 0 && current_type != SHADER_TYPE_VOLUME)
441                         return true;
442                 if(strcmp(input->name, "Displacement") == 0 && current_type != SHADER_TYPE_DISPLACEMENT)
443                         return true;
444                 if(strcmp(input->name, "Normal") == 0)
445                         return true;
446         }
447         else if(node->name == ustring("bump")) {
448                 if(strcmp(input->name, "Height") == 0)
449                         return true;
450         }
451         else if(current_type == SHADER_TYPE_DISPLACEMENT && input->link && input->link->parent->name == ustring("bump"))
452                 return true;
453
454         return false;
455 }
456
457 void OSLCompiler::add(ShaderNode *node, const char *name, bool isfilepath)
458 {
459         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
460
461         /* load filepath */
462         if(isfilepath) {
463                 name = ((OSLShaderManager*)manager)->shader_load_filepath(name);
464
465                 if(name == NULL)
466                         return;
467         }
468
469         /* pass in fixed parameter values */
470         foreach(ShaderInput *input, node->inputs) {
471                 if(!input->link) {
472                         /* checks to untangle graphs */
473                         if(node_skip_input(node, input))
474                                 continue;
475                         /* already has default value assigned */
476                         else if(input->default_value != ShaderInput::NONE)
477                                 continue;
478
479                         string param_name = compatible_name(node, input);
480                         switch(input->type) {
481                                 case SHADER_SOCKET_COLOR:
482                                         parameter_color(param_name.c_str(), input->value);
483                                         break;
484                                 case SHADER_SOCKET_POINT:
485                                         parameter_point(param_name.c_str(), input->value);
486                                         break;
487                                 case SHADER_SOCKET_VECTOR:
488                                         parameter_vector(param_name.c_str(), input->value);
489                                         break;
490                                 case SHADER_SOCKET_NORMAL:
491                                         parameter_normal(param_name.c_str(), input->value);
492                                         break;
493                                 case SHADER_SOCKET_FLOAT:
494                                         parameter(param_name.c_str(), input->value.x);
495                                         break;
496                                 case SHADER_SOCKET_INT:
497                                         parameter(param_name.c_str(), (int)input->value.x);
498                                         break;
499                                 case SHADER_SOCKET_STRING:
500                                         parameter(param_name.c_str(), input->value_string);
501                                         break;
502                                 case SHADER_SOCKET_CLOSURE:
503                                 case SHADER_SOCKET_UNDEFINED:
504                                         break;
505                         }
506                 }
507         }
508
509         /* create shader of the appropriate type. we pass "surface" to all shaders,
510          * because "volume" and "displacement" don't work yet in OSL. the shaders
511          * work fine, but presumably these values would be used for more strict
512          * checking, so when that is fixed, we should update the code here too. */
513         if(current_type == SHADER_TYPE_SURFACE)
514                 ss->Shader("surface", name, id(node).c_str());
515         else if(current_type == SHADER_TYPE_VOLUME)
516                 ss->Shader("surface", name, id(node).c_str());
517         else if(current_type == SHADER_TYPE_DISPLACEMENT)
518                 ss->Shader("surface", name, id(node).c_str());
519         else
520                 assert(0);
521         
522         /* link inputs to other nodes */
523         foreach(ShaderInput *input, node->inputs) {
524                 if(input->link) {
525                         if(node_skip_input(node, input))
526                                 continue;
527
528                         /* connect shaders */
529                         string id_from = id(input->link->parent);
530                         string id_to = id(node);
531                         string param_from = compatible_name(input->link->parent, input->link);
532                         string param_to = compatible_name(node, input);
533
534                         ss->ConnectShaders(id_from.c_str(), param_from.c_str(), id_to.c_str(), param_to.c_str());
535                 }
536         }
537
538         /* test if we shader contains specific closures */
539         OSLShaderInfo *info = ((OSLShaderManager*)manager)->shader_loaded_info(name);
540
541         if(info) {
542                 if(info->has_surface_emission)
543                         current_shader->has_surface_emission = true;
544                 if(info->has_surface_transparent)
545                         current_shader->has_surface_transparent = true;
546                 if(info->has_surface_bssrdf)
547                         current_shader->has_surface_bssrdf = true;
548         }
549 }
550
551 void OSLCompiler::parameter(const char *name, float f)
552 {
553         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
554         ss->Parameter(name, TypeDesc::TypeFloat, &f);
555 }
556
557 void OSLCompiler::parameter_color(const char *name, float3 f)
558 {
559         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
560         ss->Parameter(name, TypeDesc::TypeColor, &f);
561 }
562
563 void OSLCompiler::parameter_point(const char *name, float3 f)
564 {
565         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
566         ss->Parameter(name, TypeDesc::TypePoint, &f);
567 }
568
569 void OSLCompiler::parameter_normal(const char *name, float3 f)
570 {
571         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
572         ss->Parameter(name, TypeDesc::TypeNormal, &f);
573 }
574
575 void OSLCompiler::parameter_vector(const char *name, float3 f)
576 {
577         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
578         ss->Parameter(name, TypeDesc::TypeVector, &f);
579 }
580
581 void OSLCompiler::parameter(const char *name, int f)
582 {
583         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
584         ss->Parameter(name, TypeDesc::TypeInt, &f);
585 }
586
587 void OSLCompiler::parameter(const char *name, const char *s)
588 {
589         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
590         ss->Parameter(name, TypeDesc::TypeString, &s);
591 }
592
593 void OSLCompiler::parameter(const char *name, ustring s)
594 {
595         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
596         const char *str = s.c_str();
597         ss->Parameter(name, TypeDesc::TypeString, &str);
598 }
599
600 void OSLCompiler::parameter(const char *name, const Transform& tfm)
601 {
602         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
603         ss->Parameter(name, TypeDesc::TypeMatrix, (float*)&tfm);
604 }
605
606 void OSLCompiler::parameter_array(const char *name, const float f[], int arraylen)
607 {
608         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
609         TypeDesc type = TypeDesc::TypeFloat;
610         type.arraylen = arraylen;
611         ss->Parameter(name, type, f);
612 }
613
614 void OSLCompiler::parameter_color_array(const char *name, const float f[][3], int arraylen)
615 {
616         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
617         TypeDesc type = TypeDesc::TypeColor;
618         type.arraylen = arraylen;
619         ss->Parameter(name, type, f);
620 }
621
622 void OSLCompiler::parameter_vector_array(const char *name, const float f[][3], int arraylen)
623 {
624         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
625         TypeDesc type = TypeDesc::TypeVector;
626         type.arraylen = arraylen;
627         ss->Parameter(name, type, f);
628 }
629
630 void OSLCompiler::parameter_normal_array(const char *name, const float f[][3], int arraylen)
631 {
632         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
633         TypeDesc type = TypeDesc::TypeNormal;
634         type.arraylen = arraylen;
635         ss->Parameter(name, type, f);
636 }
637
638 void OSLCompiler::parameter_point_array(const char *name, const float f[][3], int arraylen)
639 {
640         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
641         TypeDesc type = TypeDesc::TypePoint;
642         type.arraylen = arraylen;
643         ss->Parameter(name, type, f);
644 }
645
646 void OSLCompiler::parameter_array(const char *name, const int f[], int arraylen)
647 {
648         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
649         TypeDesc type = TypeDesc::TypeInt;
650         type.arraylen = arraylen;
651         ss->Parameter(name, type, f);
652 }
653
654 void OSLCompiler::parameter_array(const char *name, const char * const s[], int arraylen)
655 {
656         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
657         TypeDesc type = TypeDesc::TypeString;
658         type.arraylen = arraylen;
659         ss->Parameter(name, type, s);
660 }
661
662 void OSLCompiler::parameter_array(const char *name, const Transform tfm[], int arraylen)
663 {
664         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
665         TypeDesc type = TypeDesc::TypeMatrix;
666         type.arraylen = arraylen;
667         ss->Parameter(name, type, (const float *)tfm);
668 }
669
670 void OSLCompiler::find_dependencies(set<ShaderNode*>& dependencies, ShaderInput *input)
671 {
672         ShaderNode *node = (input->link)? input->link->parent: NULL;
673
674         if(node) {
675                 foreach(ShaderInput *in, node->inputs)
676                         if(!node_skip_input(node, in))
677                                 find_dependencies(dependencies, in);
678
679                 dependencies.insert(node);
680         }
681 }
682
683 void OSLCompiler::generate_nodes(const set<ShaderNode*>& nodes)
684 {
685         set<ShaderNode*> done;
686         bool nodes_done;
687
688         do {
689                 nodes_done = true;
690
691                 foreach(ShaderNode *node, nodes) {
692                         if(done.find(node) == done.end()) {
693                                 bool inputs_done = true;
694
695                                 foreach(ShaderInput *input, node->inputs)
696                                         if(!node_skip_input(node, input))
697                                                 if(input->link && done.find(input->link->parent) == done.end())
698                                                         inputs_done = false;
699
700                                 if(inputs_done) {
701                                         node->compile(*this);
702                                         done.insert(node);
703
704                                         if(node->has_surface_emission())
705                                                 current_shader->has_surface_emission = true;
706                                         if(node->has_surface_transparent())
707                                                 current_shader->has_surface_transparent = true;
708                                         if(node->has_surface_bssrdf())
709                                                 current_shader->has_surface_bssrdf = true;
710                                 }
711                                 else
712                                         nodes_done = false;
713                         }
714                 }
715         } while(!nodes_done);
716 }
717
718 void OSLCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
719 {
720         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
721
722         current_type = type;
723
724         ss->ShaderGroupBegin(shader->name.c_str());
725
726         ShaderNode *output = graph->output();
727         set<ShaderNode*> dependencies;
728
729         if(type == SHADER_TYPE_SURFACE) {
730                 /* generate surface shader */
731                 find_dependencies(dependencies, output->input("Surface"));
732                 generate_nodes(dependencies);
733                 output->compile(*this);
734         }
735         else if(type == SHADER_TYPE_VOLUME) {
736                 /* generate volume shader */
737                 find_dependencies(dependencies, output->input("Volume"));
738                 generate_nodes(dependencies);
739                 output->compile(*this);
740         }
741         else if(type == SHADER_TYPE_DISPLACEMENT) {
742                 /* generate displacement shader */
743                 find_dependencies(dependencies, output->input("Displacement"));
744                 generate_nodes(dependencies);
745                 output->compile(*this);
746         }
747         else
748                 assert(0);
749
750         ss->ShaderGroupEnd();
751 }
752
753 void OSLCompiler::compile(OSLGlobals *og, Shader *shader)
754 {
755         if(shader->need_update) {
756                 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
757                 ShaderGraph *graph = shader->graph;
758                 ShaderNode *output = (graph)? graph->output(): NULL;
759
760                 /* copy graph for shader with bump mapping */
761                 if(output->input("Surface")->link && output->input("Displacement")->link)
762                         if(!shader->graph_bump)
763                                 shader->graph_bump = shader->graph->copy();
764
765                 /* finalize */
766                 shader->graph->finalize(false, true);
767                 if(shader->graph_bump)
768                         shader->graph_bump->finalize(true, true);
769
770                 current_shader = shader;
771
772                 shader->has_surface = false;
773                 shader->has_surface_emission = false;
774                 shader->has_surface_transparent = false;
775                 shader->has_surface_bssrdf = false;
776                 shader->has_volume = false;
777                 shader->has_displacement = false;
778
779                 /* generate surface shader */
780                 if(shader->used && graph && output->input("Surface")->link) {
781                         compile_type(shader, shader->graph, SHADER_TYPE_SURFACE);
782                         shader->osl_surface_ref = ss->state();
783
784                         if(shader->graph_bump) {
785                                 ss->clear_state();
786                                 compile_type(shader, shader->graph_bump, SHADER_TYPE_SURFACE);
787                                 shader->osl_surface_bump_ref = ss->state();
788                         }
789                         else
790                                 shader->osl_surface_bump_ref = shader->osl_surface_ref;
791
792                         ss->clear_state();
793
794                         shader->has_surface = true;
795                 }
796                 else {
797                         shader->osl_surface_ref = OSL::ShadingAttribStateRef();
798                         shader->osl_surface_bump_ref = OSL::ShadingAttribStateRef();
799                 }
800
801                 /* generate volume shader */
802                 if(shader->used && graph && output->input("Volume")->link) {
803                         compile_type(shader, shader->graph, SHADER_TYPE_VOLUME);
804                         shader->has_volume = true;
805
806                         shader->osl_volume_ref = ss->state();
807                         ss->clear_state();
808                 }
809                 else
810                         shader->osl_volume_ref = OSL::ShadingAttribStateRef();
811
812                 /* generate displacement shader */
813                 if(shader->used && graph && output->input("Displacement")->link) {
814                         compile_type(shader, shader->graph, SHADER_TYPE_DISPLACEMENT);
815                         shader->has_displacement = true;
816                         shader->osl_displacement_ref = ss->state();
817                         ss->clear_state();
818                 }
819                 else
820                         shader->osl_displacement_ref = OSL::ShadingAttribStateRef();
821         }
822
823         /* push state to array for lookup */
824         og->surface_state.push_back(shader->osl_surface_ref);
825         og->surface_state.push_back(shader->osl_surface_bump_ref);
826
827         og->volume_state.push_back(shader->osl_volume_ref);
828         og->volume_state.push_back(shader->osl_volume_ref);
829
830         og->displacement_state.push_back(shader->osl_displacement_ref);
831         og->displacement_state.push_back(shader->osl_displacement_ref);
832 }
833
834 #else
835
836 void OSLCompiler::add(ShaderNode *node, const char *name, bool isfilepath)
837 {
838 }
839
840 void OSLCompiler::parameter(const char *name, float f)
841 {
842 }
843
844 void OSLCompiler::parameter_color(const char *name, float3 f)
845 {
846 }
847
848 void OSLCompiler::parameter_vector(const char *name, float3 f)
849 {
850 }
851
852 void OSLCompiler::parameter_point(const char *name, float3 f)
853 {
854 }
855
856 void OSLCompiler::parameter_normal(const char *name, float3 f)
857 {
858 }
859
860 void OSLCompiler::parameter(const char *name, int f)
861 {
862 }
863
864 void OSLCompiler::parameter(const char *name, const char *s)
865 {
866 }
867
868 void OSLCompiler::parameter(const char *name, ustring s)
869 {
870 }
871
872 void OSLCompiler::parameter(const char *name, const Transform& tfm)
873 {
874 }
875
876 void OSLCompiler::parameter_array(const char *name, const float f[], int arraylen)
877 {
878 }
879
880 void OSLCompiler::parameter_color_array(const char *name, const float f[][3], int arraylen)
881 {
882 }
883
884 void OSLCompiler::parameter_vector_array(const char *name, const float f[][3], int arraylen)
885 {
886 }
887
888 void OSLCompiler::parameter_normal_array(const char *name, const float f[][3], int arraylen)
889 {
890 }
891
892 void OSLCompiler::parameter_point_array(const char *name, const float f[][3], int arraylen)
893 {
894 }
895
896 void OSLCompiler::parameter_array(const char *name, const int f[], int arraylen)
897 {
898 }
899
900 void OSLCompiler::parameter_array(const char *name, const char * const s[], int arraylen)
901 {
902 }
903
904 void OSLCompiler::parameter_array(const char *name, const Transform tfm[], int arraylen)
905 {
906 }
907
908 #endif /* WITH_OSL */
909
910 CCL_NAMESPACE_END
911