Fix incorrect OSL raytype bits and add some comments.
[blender.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/device.h"
18
19 #include "render/graph.h"
20 #include "render/light.h"
21 #include "render/osl.h"
22 #include "render/scene.h"
23 #include "render/shader.h"
24 #include "render/nodes.h"
25
26 #ifdef WITH_OSL
27
28 #include "kernel/osl/osl_globals.h"
29 #include "kernel/osl/osl_services.h"
30 #include "kernel/osl/osl_shader.h"
31
32 #include "util/util_foreach.h"
33 #include "util/util_logging.h"
34 #include "util/util_md5.h"
35 #include "util/util_path.h"
36 #include "util/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         VLOG(1) << "Total " << scene->shaders.size() << " shaders.";
82
83         device_free(device, dscene, scene);
84
85         /* determine which shaders are in use */
86         device_update_shaders_used(scene);
87
88         /* create shaders */
89         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
90
91         foreach(Shader *shader, scene->shaders) {
92                 assert(shader->graph);
93
94                 if(progress.get_cancel()) return;
95
96                 /* we can only compile one shader at the time as the OSL ShadingSytem
97                  * has a single state, but we put the lock here so different renders can
98                  * compile shaders alternating */
99                 thread_scoped_lock lock(ss_mutex);
100
101                 OSLCompiler compiler((void*)this, (void*)ss, scene->image_manager);
102                 compiler.background = (shader == scene->default_background);
103                 compiler.compile(scene, og, shader);
104
105                 if(shader->use_mis && shader->has_surface_emission)
106                         scene->light_manager->need_update = true;
107         }
108
109         /* setup shader engine */
110         og->ss = ss;
111         og->ts = ts;
112         og->services = services;
113
114         int background_id = scene->shader_manager->get_shader_id(scene->default_background);
115         og->background_state = og->surface_state[background_id & SHADER_MASK];
116         og->use = true;
117
118         foreach(Shader *shader, scene->shaders)
119                 shader->need_update = false;
120
121         need_update = false;
122         
123         /* set texture system */
124         scene->image_manager->set_osl_texture_system((void*)ts);
125
126         device_update_common(device, dscene, scene, progress);
127
128         {
129                 /* Perform greedyjit optimization.
130                  *
131                  * This might waste time on optimizing gorups which are never actually
132                  * used, but this prevents OSL from allocating data on TLS at render
133                  * time.
134                  *
135                  * This is much better for us because this way we aren't required to
136                  * stop task scheduler threads to make sure all TLS is clean and don't
137                  * have issues with TLS data free accessing freed memory if task scheduler
138                  * is being freed after the Session is freed.
139                  */
140                 thread_scoped_lock lock(ss_shared_mutex);
141                 ss->optimize_all_groups();
142         }
143 }
144
145 void OSLShaderManager::device_free(Device *device, DeviceScene *dscene, Scene *scene)
146 {
147         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
148
149         device_free_common(device, dscene, scene);
150
151         /* clear shader engine */
152         og->use = false;
153         og->ss = NULL;
154         og->ts = NULL;
155
156         og->surface_state.clear();
157         og->volume_state.clear();
158         og->displacement_state.clear();
159         og->bump_state.clear();
160         og->background_state.reset();
161 }
162
163 void OSLShaderManager::texture_system_init()
164 {
165         /* create texture system, shared between different renders to reduce memory usage */
166         thread_scoped_lock lock(ts_shared_mutex);
167
168         if(ts_shared_users == 0) {
169                 ts_shared = TextureSystem::create(true);
170
171                 ts_shared->attribute("automip",  1);
172                 ts_shared->attribute("autotile", 64);
173                 ts_shared->attribute("gray_to_rgb", 1);
174
175                 /* effectively unlimited for now, until we support proper mipmap lookups */
176                 ts_shared->attribute("max_memory_MB", 16384);
177         }
178
179         ts = ts_shared;
180         ts_shared_users++;
181 }
182
183 void OSLShaderManager::texture_system_free()
184 {
185         /* shared texture system decrease users and destroy if no longer used */
186         thread_scoped_lock lock(ts_shared_mutex);
187         ts_shared_users--;
188
189         if(ts_shared_users == 0) {
190                 ts_shared->invalidate_all(true);
191                 OSL::TextureSystem::destroy(ts_shared);
192                 ts_shared = NULL;
193         }
194
195         ts = NULL;
196 }
197
198 void OSLShaderManager::shading_system_init()
199 {
200         /* create shading system, shared between different renders to reduce memory usage */
201         thread_scoped_lock lock(ss_shared_mutex);
202
203         if(ss_shared_users == 0) {
204                 services_shared = new OSLRenderServices();
205
206                 string shader_path = path_get("shader");
207 #ifdef _WIN32
208                 /* Annoying thing, Cycles stores paths in UTF-8 codepage, so it can
209                  * operate with file paths with any character. This requires to use wide
210                  * char functions, but OSL uses old fashioned ANSI functions which means:
211                  *
212                  * - We have to convert our paths to ANSI before passing to OSL
213                  * - OSL can't be used when there's a multi-byte character in the path
214                  *   to the shaders folder.
215                  */
216                 shader_path = string_to_ansi(shader_path);
217 #endif
218
219                 ss_shared = new OSL::ShadingSystem(services_shared, ts_shared, &errhandler);
220                 ss_shared->attribute("lockgeom", 1);
221                 ss_shared->attribute("commonspace", "world");
222                 ss_shared->attribute("searchpath:shader", shader_path);
223                 ss_shared->attribute("greedyjit", 1);
224
225                 VLOG(1) << "Using shader search path: " << shader_path;
226
227                 /* our own ray types */
228                 static const char *raytypes[] = {
229                         "camera",                       /* PATH_RAY_CAMERA */
230                         "reflection",           /* PATH_RAY_REFLECT */
231                         "refraction",           /* PATH_RAY_TRANSMIT */
232                         "diffuse",                      /* PATH_RAY_DIFFUSE */
233                         "glossy",                       /* PATH_RAY_GLOSSY */
234                         "singular",                     /* PATH_RAY_SINGULAR */
235                         "transparent",          /* PATH_RAY_TRANSPARENT */
236
237                         "shadow",                       /* PATH_RAY_SHADOW_OPAQUE_NON_CATCHER */
238                         "shadow",                       /* PATH_RAY_SHADOW_OPAQUE_CATCHER */
239                         "shadow",                       /* PATH_RAY_SHADOW_TRANSPARENT_NON_CATCHER */
240                         "shadow",                       /* PATH_RAY_SHADOW_TRANSPARENT_CATCHER */
241
242                         "__unused__",
243                         "volume_scatter",       /* PATH_RAY_VOLUME_SCATTER */
244                         "__unused__",
245
246                         "__unused__",
247                         "diffuse_ancestor",     /* PATH_RAY_DIFFUSE_ANCESTOR */
248                         "__unused__",
249                         "__unused__",
250                         "__unused__",
251                         "__unused__",
252                 };
253
254                 const int nraytypes = sizeof(raytypes)/sizeof(raytypes[0]);
255                 ss_shared->attribute("raytypes", TypeDesc(TypeDesc::STRING, nraytypes), raytypes);
256
257                 OSLShader::register_closures((OSLShadingSystem*)ss_shared);
258
259                 loaded_shaders.clear();
260         }
261
262         ss = ss_shared;
263         services = services_shared;
264         ss_shared_users++;
265 }
266
267 void OSLShaderManager::shading_system_free()
268 {
269         /* shared shading system decrease users and destroy if no longer used */
270         thread_scoped_lock lock(ss_shared_mutex);
271         ss_shared_users--;
272
273         if(ss_shared_users == 0) {
274                 delete ss_shared;
275                 ss_shared = NULL;
276
277                 delete services_shared;
278                 services_shared = NULL;
279         }
280
281         ss = NULL;
282         services = NULL;
283 }
284
285 bool OSLShaderManager::osl_compile(const string& inputfile, const string& outputfile)
286 {
287         vector<string> options;
288         string stdosl_path;
289         string shader_path = path_get("shader");
290
291         /* specify output file name */
292         options.push_back("-o");
293         options.push_back(outputfile);
294
295         /* specify standard include path */
296         string include_path_arg = string("-I") + shader_path;
297         options.push_back(include_path_arg);
298
299         stdosl_path = path_get("shader/stdosl.h");
300
301         /* compile */
302         OSL::OSLCompiler *compiler = new OSL::OSLCompiler(&OSL::ErrorHandler::default_handler());
303         bool ok = compiler->compile(string_view(inputfile), options, string_view(stdosl_path));
304         delete compiler;
305
306         return ok;
307 }
308
309 bool OSLShaderManager::osl_query(OSL::OSLQuery& query, const string& filepath)
310 {
311         string searchpath = path_user_get("shaders");
312         return query.open(filepath, searchpath);
313 }
314
315 static string shader_filepath_hash(const string& filepath, uint64_t modified_time)
316 {
317         /* compute a hash from filepath and modified time to detect changes */
318         MD5Hash md5;
319         md5.append((const uint8_t*)filepath.c_str(), filepath.size());
320         md5.append((const uint8_t*)&modified_time, sizeof(modified_time));
321
322         return md5.get_hex();
323 }
324
325 const char *OSLShaderManager::shader_test_loaded(const string& hash)
326 {
327         map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
328         return (it == loaded_shaders.end())? NULL: it->first.c_str();
329 }
330
331 OSLShaderInfo *OSLShaderManager::shader_loaded_info(const string& hash)
332 {
333         map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
334         return (it == loaded_shaders.end())? NULL: &it->second;
335 }
336
337 const char *OSLShaderManager::shader_load_filepath(string filepath)
338 {
339         size_t len = filepath.size();
340         string extension = filepath.substr(len - 4);
341         uint64_t modified_time = path_modified_time(filepath);
342
343         if(extension == ".osl") {
344                 /* .OSL File */
345                 string osopath = filepath.substr(0, len - 4) + ".oso";
346                 uint64_t oso_modified_time = path_modified_time(osopath);
347
348                 /* test if we have loaded the corresponding .OSO already */
349                 if(oso_modified_time != 0) {
350                         const char *hash = shader_test_loaded(shader_filepath_hash(osopath, oso_modified_time));
351
352                         if(hash)
353                                 return hash;
354                 }
355
356                 /* autocompile .OSL to .OSO if needed */
357                 if(oso_modified_time == 0 || (oso_modified_time < modified_time)) {
358                         OSLShaderManager::osl_compile(filepath, osopath);
359                         modified_time = path_modified_time(osopath);
360                 }
361                 else
362                         modified_time = oso_modified_time;
363
364                 filepath = osopath;
365         }
366         else {
367                 if(extension == ".oso") {
368                         /* .OSO File, nothing to do */
369                 }
370                 else if(path_dirname(filepath) == "") {
371                         /* .OSO File in search path */
372                         filepath = path_join(path_user_get("shaders"), filepath + ".oso");
373                 }
374                 else {
375                         /* unknown file */
376                         return NULL;
377                 }
378
379                 /* test if we have loaded this .OSO already */
380                 const char *hash = shader_test_loaded(shader_filepath_hash(filepath, modified_time));
381
382                 if(hash)
383                         return hash;
384         }
385
386         /* read oso bytecode from file */
387         string bytecode_hash = shader_filepath_hash(filepath, modified_time);
388         string bytecode;
389
390         if(!path_read_text(filepath, bytecode)) {
391                 fprintf(stderr, "Cycles shader graph: failed to read file %s\n", filepath.c_str());
392                 OSLShaderInfo info;
393                 loaded_shaders[bytecode_hash] = info; /* to avoid repeat tries */
394                 return NULL;
395         }
396
397         return shader_load_bytecode(bytecode_hash, bytecode);
398 }
399
400 const char *OSLShaderManager::shader_load_bytecode(const string& hash, const string& bytecode)
401 {
402         ss->LoadMemoryCompiledShader(hash.c_str(), bytecode.c_str());
403
404         OSLShaderInfo info;
405
406         if(!info.query.open_bytecode(bytecode)) {
407                 fprintf(stderr, "OSL query error: %s\n", info.query.geterror().c_str());
408         }
409
410         /* this is a bit weak, but works */
411         info.has_surface_emission = (bytecode.find("\"emission\"") != string::npos);
412         info.has_surface_transparent = (bytecode.find("\"transparent\"") != string::npos);
413         info.has_surface_bssrdf = (bytecode.find("\"bssrdf\"") != string::npos);
414
415         loaded_shaders[hash] = info;
416
417         return loaded_shaders.find(hash)->first.c_str();
418 }
419
420 OSLNode *OSLShaderManager::osl_node(const std::string& filepath,
421                                     const std::string& bytecode_hash,
422                                     const std::string& bytecode)
423 {
424         /* create query */
425         const char *hash;
426
427         if(!filepath.empty()) {
428                 hash = shader_load_filepath(filepath);
429         }
430         else {
431                 hash = shader_test_loaded(bytecode_hash);
432                 if(!hash)
433                         hash = shader_load_bytecode(bytecode_hash, bytecode);
434         }
435
436         if(!hash) {
437                 return NULL;
438         }
439
440         OSLShaderInfo *info = shader_loaded_info(hash);
441
442         /* count number of inputs */
443         size_t num_inputs = 0;
444
445         for(int i = 0; i < info->query.nparams(); i++) {
446                 const OSL::OSLQuery::Parameter *param = info->query.getparam(i);
447
448                 /* skip unsupported types */
449                 if(param->varlenarray || param->isstruct || param->type.arraylen > 1)
450                         continue;
451
452                 if(!param->isoutput)
453                         num_inputs++;
454         }
455
456         /* create node */
457         OSLNode *node = OSLNode::create(num_inputs);
458
459         /* add new sockets from parameters */
460         set<void*> used_sockets;
461
462         for(int i = 0; i < info->query.nparams(); i++) {
463                 const OSL::OSLQuery::Parameter *param = info->query.getparam(i);
464
465                 /* skip unsupported types */
466                 if(param->varlenarray || param->isstruct || param->type.arraylen > 1)
467                         continue;
468
469                 SocketType::Type socket_type;
470
471                 if(param->isclosure) {
472                         socket_type = SocketType::CLOSURE;
473                 }
474                 else if(param->type.vecsemantics != TypeDesc::NOSEMANTICS) {
475                         if(param->type.vecsemantics == TypeDesc::COLOR)
476                                 socket_type = SocketType::COLOR;
477                         else if(param->type.vecsemantics == TypeDesc::POINT)
478                                 socket_type = SocketType::POINT;
479                         else if(param->type.vecsemantics == TypeDesc::VECTOR)
480                                 socket_type = SocketType::VECTOR;
481                         else if(param->type.vecsemantics == TypeDesc::NORMAL)
482                                 socket_type = SocketType::NORMAL;
483                         else
484                                 continue;
485
486                         if(!param->isoutput && param->validdefault) {
487                                 float3 *default_value = (float3*)node->input_default_value();
488                                 default_value->x = param->fdefault[0];
489                                 default_value->y = param->fdefault[1];
490                                 default_value->z = param->fdefault[2];
491                         }
492                 }
493                 else if(param->type.aggregate == TypeDesc::SCALAR) {
494                         if(param->type.basetype == TypeDesc::INT) {
495                                 socket_type = SocketType::INT;
496
497                                 if(!param->isoutput && param->validdefault) {
498                                         *(int*)node->input_default_value() = param->idefault[0];
499                                 }
500                         }
501                         else if(param->type.basetype == TypeDesc::FLOAT) {
502                                 socket_type = SocketType::FLOAT;
503
504                                 if(!param->isoutput && param->validdefault) {
505                                         *(float*)node->input_default_value() = param->fdefault[0];
506                                 }
507                         }
508                         else if(param->type.basetype == TypeDesc::STRING) {
509                                 socket_type = SocketType::STRING;
510
511                                 if(!param->isoutput && param->validdefault) {
512                                         *(ustring*)node->input_default_value() = param->sdefault[0];
513                                 }
514                         }
515                         else
516                                 continue;
517                 }
518                 else
519                         continue;
520
521                 if(param->isoutput) {
522                         node->add_output(param->name, socket_type);
523                 }
524                 else {
525                         node->add_input(param->name, socket_type);
526                 }
527         }
528
529         /* set bytcode hash or filepath */
530         if(!bytecode_hash.empty()) {
531                 node->bytecode_hash = bytecode_hash;
532         }
533         else {
534                 node->filepath = filepath;
535         }
536
537         /* Generate inputs and outputs */
538         node->create_inputs_outputs(node->type);
539
540         return node;
541 }
542
543 /* Graph Compiler */
544
545 OSLCompiler::OSLCompiler(void *manager_, void *shadingsys_, ImageManager *image_manager_)
546 {
547         manager = manager_;
548         shadingsys = shadingsys_;
549         image_manager = image_manager_;
550         current_type = SHADER_TYPE_SURFACE;
551         current_shader = NULL;
552         background = false;
553 }
554
555 string OSLCompiler::id(ShaderNode *node)
556 {
557         /* assign layer unique name based on pointer address + bump mode */
558         stringstream stream;
559         stream << "node_" << node->type->name << "_" << node;
560
561         return stream.str();
562 }
563
564 string OSLCompiler::compatible_name(ShaderNode *node, ShaderInput *input)
565 {
566         string sname(input->name().string());
567         size_t i;
568
569         /* strip whitespace */
570         while((i = sname.find(" ")) != string::npos)
571                 sname.replace(i, 1, "");
572         
573         /* if output exists with the same name, add "In" suffix */
574         foreach(ShaderOutput *output, node->outputs) {
575                 if(input->name() == output->name()) {
576                         sname += "In";
577                         break;
578                 }
579         }
580         
581         return sname;
582 }
583
584 string OSLCompiler::compatible_name(ShaderNode *node, ShaderOutput *output)
585 {
586         string sname(output->name().string());
587         size_t i;
588
589         /* strip whitespace */
590         while((i = sname.find(" ")) != string::npos)
591                 sname.replace(i, 1, "");
592         
593         /* if input exists with the same name, add "Out" suffix */
594         foreach(ShaderInput *input, node->inputs) {
595                 if(input->name() == output->name()) {
596                         sname += "Out";
597                         break;
598                 }
599         }
600         
601         return sname;
602 }
603
604 bool OSLCompiler::node_skip_input(ShaderNode *node, ShaderInput *input)
605 {
606         /* exception for output node, only one input is actually used
607          * depending on the current shader type */
608         
609         if(input->flags() & SocketType::SVM_INTERNAL)
610                 return true;
611
612         if(node->special_type == SHADER_SPECIAL_TYPE_OUTPUT) {
613                 if(input->name() == "Surface" && current_type != SHADER_TYPE_SURFACE)
614                         return true;
615                 if(input->name() == "Volume" && current_type != SHADER_TYPE_VOLUME)
616                         return true;
617                 if(input->name() == "Displacement" && current_type != SHADER_TYPE_DISPLACEMENT)
618                         return true;
619                 if(input->name() == "Normal" && current_type != SHADER_TYPE_BUMP)
620                         return true;
621         }
622         else if(node->special_type == SHADER_SPECIAL_TYPE_BUMP) {
623                 if(input->name() == "Height")
624                         return true;
625         }
626         else if(current_type == SHADER_TYPE_DISPLACEMENT && input->link && input->link->parent->special_type == SHADER_SPECIAL_TYPE_BUMP)
627                 return true;
628
629         return false;
630 }
631
632 void OSLCompiler::add(ShaderNode *node, const char *name, bool isfilepath)
633 {
634         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
635
636         /* load filepath */
637         if(isfilepath) {
638                 name = ((OSLShaderManager*)manager)->shader_load_filepath(name);
639
640                 if(name == NULL)
641                         return;
642         }
643
644         /* pass in fixed parameter values */
645         foreach(ShaderInput *input, node->inputs) {
646                 if(!input->link) {
647                         /* checks to untangle graphs */
648                         if(node_skip_input(node, input))
649                                 continue;
650                         /* already has default value assigned */
651                         else if(input->flags() & SocketType::DEFAULT_LINK_MASK)
652                                 continue;
653
654                         string param_name = compatible_name(node, input);
655                         const SocketType& socket = input->socket_type;
656                         switch(input->type()) {
657                                 case SocketType::COLOR:
658                                         parameter_color(param_name.c_str(), node->get_float3(socket));
659                                         break;
660                                 case SocketType::POINT:
661                                         parameter_point(param_name.c_str(), node->get_float3(socket));
662                                         break;
663                                 case SocketType::VECTOR:
664                                         parameter_vector(param_name.c_str(), node->get_float3(socket));
665                                         break;
666                                 case SocketType::NORMAL:
667                                         parameter_normal(param_name.c_str(), node->get_float3(socket));
668                                         break;
669                                 case SocketType::FLOAT:
670                                         parameter(param_name.c_str(), node->get_float(socket));
671                                         break;
672                                 case SocketType::INT:
673                                         parameter(param_name.c_str(), node->get_int(socket));
674                                         break;
675                                 case SocketType::STRING:
676                                         parameter(param_name.c_str(), node->get_string(socket));
677                                         break;
678                                 case SocketType::CLOSURE:
679                                 case SocketType::UNDEFINED:
680                                 default:
681                                         break;
682                         }
683                 }
684         }
685
686         /* create shader of the appropriate type. OSL only distinguishes between "surface"
687          * and "displacement" atm */
688         if(current_type == SHADER_TYPE_SURFACE)
689                 ss->Shader("surface", name, id(node).c_str());
690         else if(current_type == SHADER_TYPE_VOLUME)
691                 ss->Shader("surface", name, id(node).c_str());
692         else if(current_type == SHADER_TYPE_DISPLACEMENT)
693                 ss->Shader("displacement", name, id(node).c_str());
694         else if(current_type == SHADER_TYPE_BUMP)
695                 ss->Shader("displacement", name, id(node).c_str());
696         else
697                 assert(0);
698         
699         /* link inputs to other nodes */
700         foreach(ShaderInput *input, node->inputs) {
701                 if(input->link) {
702                         if(node_skip_input(node, input))
703                                 continue;
704
705                         /* connect shaders */
706                         string id_from = id(input->link->parent);
707                         string id_to = id(node);
708                         string param_from = compatible_name(input->link->parent, input->link);
709                         string param_to = compatible_name(node, input);
710
711                         ss->ConnectShaders(id_from.c_str(), param_from.c_str(), id_to.c_str(), param_to.c_str());
712                 }
713         }
714
715         /* test if we shader contains specific closures */
716         OSLShaderInfo *info = ((OSLShaderManager*)manager)->shader_loaded_info(name);
717
718         if(current_type == SHADER_TYPE_SURFACE) {
719                 if(info) {
720                         if(info->has_surface_emission)
721                                 current_shader->has_surface_emission = true;
722                         if(info->has_surface_transparent)
723                                 current_shader->has_surface_transparent = true;
724                         if(info->has_surface_bssrdf) {
725                                 current_shader->has_surface_bssrdf = true;
726                                 current_shader->has_bssrdf_bump = true; /* can't detect yet */
727                         }
728                         current_shader->has_bump = true; /* can't detect yet */
729                 }
730
731                 if(node->has_spatial_varying()) {
732                         current_shader->has_surface_spatial_varying = true;
733                 }
734         }
735         else if(current_type == SHADER_TYPE_VOLUME) {
736                 if(node->has_spatial_varying())
737                         current_shader->has_volume_spatial_varying = true;
738         }
739
740         if(node->has_object_dependency()) {
741                 current_shader->has_object_dependency = true;
742         }
743
744         if(node->has_integrator_dependency()) {
745                 current_shader->has_integrator_dependency = true;
746         }
747 }
748
749 static TypeDesc array_typedesc(TypeDesc typedesc, int arraylength)
750 {
751         return TypeDesc((TypeDesc::BASETYPE)typedesc.basetype,
752                         (TypeDesc::AGGREGATE)typedesc.aggregate,
753                         (TypeDesc::VECSEMANTICS)typedesc.vecsemantics,
754                         arraylength);
755 }
756
757 void OSLCompiler::parameter(ShaderNode* node, const char *name)
758 {
759         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
760         ustring uname = ustring(name);
761         const SocketType& socket = *(node->type->find_input(uname));
762
763         switch(socket.type)
764         {
765                 case SocketType::BOOLEAN:
766                 {
767                         int value = node->get_bool(socket);
768                         ss->Parameter(name, TypeDesc::TypeInt, &value);
769                         break;
770                 }
771                 case SocketType::FLOAT:
772                 {
773                         float value = node->get_float(socket);
774                         ss->Parameter(uname, TypeDesc::TypeFloat, &value);
775                         break;
776                 }
777                 case SocketType::INT:
778                 {
779                         int value = node->get_int(socket);
780                         ss->Parameter(uname, TypeDesc::TypeInt, &value);
781                         break;
782                 }
783                 case SocketType::COLOR:
784                 {
785                         float3 value = node->get_float3(socket);
786                         ss->Parameter(uname, TypeDesc::TypeColor, &value);
787                         break;
788                 }
789                 case SocketType::VECTOR:
790                 {
791                         float3 value = node->get_float3(socket);
792                         ss->Parameter(uname, TypeDesc::TypeVector, &value);
793                         break;
794                 }
795                 case SocketType::POINT:
796                 {
797                         float3 value = node->get_float3(socket);
798                         ss->Parameter(uname, TypeDesc::TypePoint, &value);
799                         break;
800                 }
801                 case SocketType::NORMAL:
802                 {
803                         float3 value = node->get_float3(socket);
804                         ss->Parameter(uname, TypeDesc::TypeNormal, &value);
805                         break;
806                 }
807                 case SocketType::POINT2:
808                 {
809                         float2 value = node->get_float2(socket);
810                         ss->Parameter(uname, TypeDesc(TypeDesc::FLOAT, TypeDesc::VEC2, TypeDesc::POINT), &value);
811                         break;
812                 }
813                 case SocketType::STRING:
814                 {
815                         ustring value = node->get_string(socket);
816                         ss->Parameter(uname, TypeDesc::TypeString, &value);
817                         break;
818                 }
819                 case SocketType::ENUM:
820                 {
821                         ustring value = node->get_string(socket);
822                         ss->Parameter(uname, TypeDesc::TypeString, &value);
823                         break;
824                 }
825                 case SocketType::TRANSFORM:
826                 {
827                         Transform value = node->get_transform(socket);
828                         ss->Parameter(uname, TypeDesc::TypeMatrix, &value);
829                         break;
830                 }
831                 case SocketType::BOOLEAN_ARRAY:
832                 {
833                         // OSL does not support booleans, so convert to int
834                         const array<bool>& value = node->get_bool_array(socket);
835                         array<int> intvalue(value.size());
836                         for(size_t i = 0; i < value.size(); i++)
837                                 intvalue[i] = value[i];
838                         ss->Parameter(uname, array_typedesc(TypeDesc::TypeInt, value.size()), intvalue.data());
839                         break;
840                 }
841                 case SocketType::FLOAT_ARRAY:
842                 {
843                         const array<float>& value = node->get_float_array(socket);
844                         ss->Parameter(uname, array_typedesc(TypeDesc::TypeFloat, value.size()), value.data());
845                         break;
846                 }
847                 case SocketType::INT_ARRAY:
848                 {
849                         const array<int>& value = node->get_int_array(socket);
850                         ss->Parameter(uname, array_typedesc(TypeDesc::TypeInt, value.size()), value.data());
851                         break;
852                 }
853                 case SocketType::COLOR_ARRAY:
854                 case SocketType::VECTOR_ARRAY:
855                 case SocketType::POINT_ARRAY:
856                 case SocketType::NORMAL_ARRAY:
857                 {
858                         TypeDesc typedesc;
859
860                         switch(socket.type)
861                         {
862                                 case SocketType::COLOR_ARRAY: typedesc = TypeDesc::TypeColor; break;
863                                 case SocketType::VECTOR_ARRAY: typedesc = TypeDesc::TypeVector; break;
864                                 case SocketType::POINT_ARRAY: typedesc = TypeDesc::TypePoint; break;
865                                 case SocketType::NORMAL_ARRAY: typedesc = TypeDesc::TypeNormal; break;
866                                 default: assert(0); break;
867                         }
868
869                         // convert to tightly packed array since float3 has padding
870                         const array<float3>& value = node->get_float3_array(socket);
871                         array<float> fvalue(value.size() * 3);
872                         for(size_t i = 0, j = 0; i < value.size(); i++) {
873                                 fvalue[j++] = value[i].x;
874                                 fvalue[j++] = value[i].y;
875                                 fvalue[j++] = value[i].z;
876                         }
877
878                         ss->Parameter(uname, array_typedesc(typedesc, value.size()), fvalue.data());
879                         break;
880                 }
881                 case SocketType::POINT2_ARRAY:
882                 {
883                         const array<float2>& value = node->get_float2_array(socket);
884                         ss->Parameter(uname, array_typedesc(TypeDesc(TypeDesc::FLOAT, TypeDesc::VEC2, TypeDesc::POINT), value.size()), value.data());
885                         break;
886                 }
887                 case SocketType::STRING_ARRAY:
888                 {
889                         const array<ustring>& value = node->get_string_array(socket);
890                         ss->Parameter(uname, array_typedesc(TypeDesc::TypeString, value.size()), value.data());
891                         break;
892                 }
893                 case SocketType::TRANSFORM_ARRAY:
894                 {
895                         const array<Transform>& value = node->get_transform_array(socket);
896                         ss->Parameter(uname, array_typedesc(TypeDesc::TypeMatrix, value.size()), value.data());
897                         break;
898                 }
899                 case SocketType::CLOSURE:
900                 case SocketType::NODE:
901                 case SocketType::NODE_ARRAY:
902                 case SocketType::UNDEFINED:
903                 case SocketType::UINT:
904                 {
905                         assert(0);
906                         break;
907                 }
908         }
909 }
910
911 void OSLCompiler::parameter(const char *name, float f)
912 {
913         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
914         ss->Parameter(name, TypeDesc::TypeFloat, &f);
915 }
916
917 void OSLCompiler::parameter_color(const char *name, float3 f)
918 {
919         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
920         ss->Parameter(name, TypeDesc::TypeColor, &f);
921 }
922
923 void OSLCompiler::parameter_point(const char *name, float3 f)
924 {
925         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
926         ss->Parameter(name, TypeDesc::TypePoint, &f);
927 }
928
929 void OSLCompiler::parameter_normal(const char *name, float3 f)
930 {
931         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
932         ss->Parameter(name, TypeDesc::TypeNormal, &f);
933 }
934
935 void OSLCompiler::parameter_vector(const char *name, float3 f)
936 {
937         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
938         ss->Parameter(name, TypeDesc::TypeVector, &f);
939 }
940
941 void OSLCompiler::parameter(const char *name, int f)
942 {
943         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
944         ss->Parameter(name, TypeDesc::TypeInt, &f);
945 }
946
947 void OSLCompiler::parameter(const char *name, const char *s)
948 {
949         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
950         ss->Parameter(name, TypeDesc::TypeString, &s);
951 }
952
953 void OSLCompiler::parameter(const char *name, ustring s)
954 {
955         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
956         const char *str = s.c_str();
957         ss->Parameter(name, TypeDesc::TypeString, &str);
958 }
959
960 void OSLCompiler::parameter(const char *name, const Transform& tfm)
961 {
962         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
963         ss->Parameter(name, TypeDesc::TypeMatrix, (float*)&tfm);
964 }
965
966 void OSLCompiler::parameter_array(const char *name, const float f[], int arraylen)
967 {
968         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
969         TypeDesc type = TypeDesc::TypeFloat;
970         type.arraylen = arraylen;
971         ss->Parameter(name, type, f);
972 }
973
974 void OSLCompiler::parameter_color_array(const char *name, const array<float3>& f)
975 {
976         /* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
977         array<float[3]> table(f.size());
978
979         for(int i = 0; i < f.size(); ++i) {
980                 table[i][0] = f[i].x;
981                 table[i][1] = f[i].y;
982                 table[i][2] = f[i].z;
983         }
984
985         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
986         TypeDesc type = TypeDesc::TypeColor;
987         type.arraylen = table.size();
988         ss->Parameter(name, type, table.data());
989 }
990
991 void OSLCompiler::find_dependencies(ShaderNodeSet& dependencies, ShaderInput *input)
992 {
993         ShaderNode *node = (input->link)? input->link->parent: NULL;
994
995         if(node != NULL && dependencies.find(node) == dependencies.end()) {
996                 foreach(ShaderInput *in, node->inputs)
997                         if(!node_skip_input(node, in))
998                                 find_dependencies(dependencies, in);
999
1000                 dependencies.insert(node);
1001         }
1002 }
1003
1004 void OSLCompiler::generate_nodes(const ShaderNodeSet& nodes)
1005 {
1006         ShaderNodeSet done;
1007         bool nodes_done;
1008
1009         do {
1010                 nodes_done = true;
1011
1012                 foreach(ShaderNode *node, nodes) {
1013                         if(done.find(node) == done.end()) {
1014                                 bool inputs_done = true;
1015
1016                                 foreach(ShaderInput *input, node->inputs)
1017                                         if(!node_skip_input(node, input))
1018                                                 if(input->link && done.find(input->link->parent) == done.end())
1019                                                         inputs_done = false;
1020
1021                                 if(inputs_done) {
1022                                         node->compile(*this);
1023                                         done.insert(node);
1024
1025                                         if(current_type == SHADER_TYPE_SURFACE) {
1026                                                 if(node->has_surface_emission())
1027                                                         current_shader->has_surface_emission = true;
1028                                                 if(node->has_surface_transparent())
1029                                                         current_shader->has_surface_transparent = true;
1030                                                 if(node->has_spatial_varying())
1031                                                         current_shader->has_surface_spatial_varying = true;
1032                                                 if(node->has_surface_bssrdf()) {
1033                                                         current_shader->has_surface_bssrdf = true;
1034                                                         if(node->has_bssrdf_bump())
1035                                                                 current_shader->has_bssrdf_bump = true;
1036                                                 }
1037                                                 if(node->has_bump()) {
1038                                                         current_shader->has_bump = true;
1039                                                 }
1040                                         }
1041                                         else if(current_type == SHADER_TYPE_VOLUME) {
1042                                                 if(node->has_spatial_varying())
1043                                                         current_shader->has_volume_spatial_varying = true;
1044                                         }
1045                                 }
1046                                 else
1047                                         nodes_done = false;
1048                         }
1049                 }
1050         } while(!nodes_done);
1051 }
1052
1053 OSL::ShaderGroupRef OSLCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
1054 {
1055         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
1056
1057         current_type = type;
1058
1059         OSL::ShaderGroupRef group = ss->ShaderGroupBegin(shader->name.c_str());
1060
1061         ShaderNode *output = graph->output();
1062         ShaderNodeSet dependencies;
1063
1064         if(type == SHADER_TYPE_SURFACE) {
1065                 /* generate surface shader */
1066                 find_dependencies(dependencies, output->input("Surface"));
1067                 generate_nodes(dependencies);
1068                 output->compile(*this);
1069         }
1070         else if(type == SHADER_TYPE_BUMP) {
1071                 /* generate bump shader */
1072                 find_dependencies(dependencies, output->input("Normal"));
1073                 generate_nodes(dependencies);
1074                 output->compile(*this);
1075         }
1076         else if(type == SHADER_TYPE_VOLUME) {
1077                 /* generate volume shader */
1078                 find_dependencies(dependencies, output->input("Volume"));
1079                 generate_nodes(dependencies);
1080                 output->compile(*this);
1081         }
1082         else if(type == SHADER_TYPE_DISPLACEMENT) {
1083                 /* generate displacement shader */
1084                 find_dependencies(dependencies, output->input("Displacement"));
1085                 generate_nodes(dependencies);
1086                 output->compile(*this);
1087         }
1088         else
1089                 assert(0);
1090
1091         ss->ShaderGroupEnd();
1092
1093         return group;
1094 }
1095
1096 void OSLCompiler::compile(Scene *scene, OSLGlobals *og, Shader *shader)
1097 {
1098         if(shader->need_update) {
1099                 ShaderGraph *graph = shader->graph;
1100                 ShaderNode *output = (graph)? graph->output(): NULL;
1101
1102                 bool has_bump = (shader->displacement_method != DISPLACE_TRUE) &&
1103                                 output->input("Surface")->link && output->input("Displacement")->link;
1104
1105                 /* finalize */
1106                 shader->graph->finalize(scene,
1107                                         has_bump,
1108                                         shader->has_integrator_dependency,
1109                                         shader->displacement_method == DISPLACE_BOTH);
1110
1111                 current_shader = shader;
1112
1113                 shader->has_surface = false;
1114                 shader->has_surface_emission = false;
1115                 shader->has_surface_transparent = false;
1116                 shader->has_surface_bssrdf = false;
1117                 shader->has_bump = has_bump;
1118                 shader->has_bssrdf_bump = has_bump;
1119                 shader->has_volume = false;
1120                 shader->has_displacement = false;
1121                 shader->has_surface_spatial_varying = false;
1122                 shader->has_volume_spatial_varying = false;
1123                 shader->has_object_dependency = false;
1124                 shader->has_integrator_dependency = false;
1125
1126                 /* generate surface shader */
1127                 if(shader->used && graph && output->input("Surface")->link) {
1128                         shader->osl_surface_ref = compile_type(shader, shader->graph, SHADER_TYPE_SURFACE);
1129
1130                         if(has_bump)
1131                                 shader->osl_surface_bump_ref = compile_type(shader, shader->graph, SHADER_TYPE_BUMP);
1132                         else
1133                                 shader->osl_surface_bump_ref = OSL::ShaderGroupRef();
1134
1135                         shader->has_surface = true;
1136                 }
1137                 else {
1138                         shader->osl_surface_ref = OSL::ShaderGroupRef();
1139                         shader->osl_surface_bump_ref = OSL::ShaderGroupRef();
1140                 }
1141
1142                 /* generate volume shader */
1143                 if(shader->used && graph && output->input("Volume")->link) {
1144                         shader->osl_volume_ref = compile_type(shader, shader->graph, SHADER_TYPE_VOLUME);
1145                         shader->has_volume = true;
1146                 }
1147                 else
1148                         shader->osl_volume_ref = OSL::ShaderGroupRef();
1149
1150                 /* generate displacement shader */
1151                 if(shader->used && graph && output->input("Displacement")->link) {
1152                         shader->osl_displacement_ref = compile_type(shader, shader->graph, SHADER_TYPE_DISPLACEMENT);
1153                         shader->has_displacement = true;
1154                 }
1155                 else
1156                         shader->osl_displacement_ref = OSL::ShaderGroupRef();
1157         }
1158
1159         /* push state to array for lookup */
1160         og->surface_state.push_back(shader->osl_surface_ref);
1161         og->volume_state.push_back(shader->osl_volume_ref);
1162         og->displacement_state.push_back(shader->osl_displacement_ref);
1163         og->bump_state.push_back(shader->osl_surface_bump_ref);
1164 }
1165
1166 #else
1167
1168 void OSLCompiler::add(ShaderNode * /*node*/, const char * /*name*/, bool /*isfilepath*/)
1169 {
1170 }
1171
1172 void OSLCompiler::parameter(ShaderNode * /*node*/, const char * /*name*/)
1173 {
1174 }
1175
1176 void OSLCompiler::parameter(const char * /*name*/, float /*f*/)
1177 {
1178 }
1179
1180 void OSLCompiler::parameter_color(const char * /*name*/, float3 /*f*/)
1181 {
1182 }
1183
1184 void OSLCompiler::parameter_vector(const char * /*name*/, float3 /*f*/)
1185 {
1186 }
1187
1188 void OSLCompiler::parameter_point(const char * /*name*/, float3 /*f*/)
1189 {
1190 }
1191
1192 void OSLCompiler::parameter_normal(const char * /*name*/, float3 /*f*/)
1193 {
1194 }
1195
1196 void OSLCompiler::parameter(const char * /*name*/, int /*f*/)
1197 {
1198 }
1199
1200 void OSLCompiler::parameter(const char * /*name*/, const char * /*s*/)
1201 {
1202 }
1203
1204 void OSLCompiler::parameter(const char * /*name*/, ustring /*s*/)
1205 {
1206 }
1207
1208 void OSLCompiler::parameter(const char * /*name*/, const Transform& /*tfm*/)
1209 {
1210 }
1211
1212 void OSLCompiler::parameter_array(const char * /*name*/, const float /*f*/[], int /*arraylen*/)
1213 {
1214 }
1215
1216 void OSLCompiler::parameter_color_array(const char * /*name*/, const array<float3>& /*f*/)
1217 {
1218 }
1219
1220 #endif /* WITH_OSL */
1221
1222 CCL_NAMESPACE_END
1223