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