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