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