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