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