446cf72f5d6da50b4bc63d72731b7ff535798673
[blender.git] / intern / cycles / render / osl.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include "device.h"
20
21 #include "graph.h"
22 #include "light.h"
23 #include "osl.h"
24 #include "scene.h"
25 #include "shader.h"
26
27 #ifdef WITH_OSL
28
29 #include "osl_globals.h"
30 #include "osl_services.h"
31 #include "osl_shader.h"
32
33 #include "util_foreach.h"
34 #include "util_path.h"
35 #include "util_progress.h"
36
37 #endif
38
39 CCL_NAMESPACE_BEGIN
40
41 #ifdef WITH_OSL
42
43 /* Shader Manager */
44
45 OSLShaderManager::OSLShaderManager()
46 {
47         services = new OSLRenderServices();
48
49         /* if we let OSL create it, it leaks */
50         ts = TextureSystem::create(true);
51         ts->attribute("automip",  1);
52         ts->attribute("autotile", 64);
53
54         ss = OSL::ShadingSystem::create(services, ts, &errhandler);
55         ss->attribute("lockgeom", 1);
56         ss->attribute("commonspace", "world");
57         ss->attribute("optimize", 2);
58         //ss->attribute("debug", 1);
59         //ss->attribute("statistics:level", 1);
60         ss->attribute("searchpath:shader", path_get("shader").c_str());
61
62         OSLShader::register_closures(ss);
63 }
64
65 OSLShaderManager::~OSLShaderManager()
66 {
67         OSL::ShadingSystem::destroy(ss);
68         OSL::TextureSystem::destroy(ts);
69         delete services;
70 }
71
72 void OSLShaderManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
73 {
74         /* test if we need to update */
75         bool need_update = false;
76
77         foreach(Shader *shader, scene->shaders)
78                 if(shader->need_update)
79                         need_update = true;
80         
81         if(!need_update)
82                 return;
83
84         device_free(device, dscene);
85
86         /* create shaders */
87         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
88
89         foreach(Shader *shader, scene->shaders) {
90                 assert(shader->graph);
91
92                 if(progress.get_cancel()) return;
93
94                 if(shader->has_surface_emission)
95                         scene->light_manager->need_update = true;
96
97                 OSLCompiler compiler((void*)ss);
98                 compiler.background = (shader == scene->shaders[scene->default_background]);
99                 compiler.compile(og, shader);
100         }
101
102         /* setup shader engine */
103         og->ss = ss;
104         int background_id = scene->shader_manager->get_shader_id(scene->default_background);
105         og->background_state = og->surface_state[background_id];
106         og->use = true;
107
108         tls_create(OSLGlobals::ThreadData, og->thread_data);
109
110         foreach(Shader *shader, scene->shaders)
111                 shader->need_update = false;
112         
113         /* set texture system */
114         scene->image_manager->set_osl_texture_system((void*)ts);
115 }
116
117 void OSLShaderManager::device_free(Device *device, DeviceScene *dscene)
118 {
119         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
120
121         /* clear shader engine */
122         og->use = false;
123         og->ss = NULL;
124
125         tls_delete(OSLGlobals::ThreadData, og->thread_data);
126
127         og->surface_state.clear();
128         og->volume_state.clear();
129         og->displacement_state.clear();
130         og->background_state.reset();
131 }
132
133 /* Graph Compiler */
134
135 OSLCompiler::OSLCompiler(void *shadingsys_)
136 {
137         shadingsys = shadingsys_;
138         current_type = SHADER_TYPE_SURFACE;
139         current_shader = NULL;
140         background = false;
141 }
142
143 string OSLCompiler::id(ShaderNode *node)
144 {
145         /* assign layer unique name based on pointer address + bump mode */
146         stringstream stream;
147         stream << "node_" << node->name << "_" << node;
148
149         return stream.str();
150 }
151
152 string OSLCompiler::compatible_name(const char *name)
153 {
154         string sname = name;
155         size_t i;
156
157         while((i = sname.find(" ")) != string::npos)
158                 sname.replace(i, 1, "");
159         
160         return sname;
161 }
162
163 bool OSLCompiler::node_skip_input(ShaderNode *node, ShaderInput *input)
164 {
165         /* exception for output node, only one input is actually used
166            depending on the current shader type */
167
168         if(node->name == ustring("output")) {
169                 if(strcmp(input->name, "Surface") == 0 && current_type != SHADER_TYPE_SURFACE)
170                         return true;
171                 if(strcmp(input->name, "Volume") == 0 && current_type != SHADER_TYPE_VOLUME)
172                         return true;
173                 if(strcmp(input->name, "Displacement") == 0 && current_type != SHADER_TYPE_DISPLACEMENT)
174                         return true;
175         }
176         else if(current_type == SHADER_TYPE_DISPLACEMENT && input->link && input->link->parent->name == ustring("bump"))
177                 return true;
178
179         return false;
180 }
181
182 void OSLCompiler::add(ShaderNode *node, const char *name)
183 {
184         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
185
186         /* pass in fixed parameter values */
187         foreach(ShaderInput *input, node->inputs) {
188                 if(!input->link) {
189                         /* checks to untangle graphs */
190                         if(node_skip_input(node, input))
191                                 continue;
192                         /* already has default value assigned */
193                         else if(input->default_value != ShaderInput::NONE)
194                                 continue;
195
196                         switch(input->type) {
197                                 case SHADER_SOCKET_COLOR:
198                                         parameter_color(input->name, input->value);
199                                         break;
200                                 case SHADER_SOCKET_POINT:
201                                         parameter_point(input->name, input->value);
202                                         break;
203                                 case SHADER_SOCKET_VECTOR:
204                                         parameter_vector(input->name, input->value);
205                                         break;
206                                 case SHADER_SOCKET_NORMAL:
207                                         parameter_normal(input->name, input->value);
208                                         break;
209                                 case SHADER_SOCKET_FLOAT:
210                                         parameter(input->name, input->value.x);
211                                         break;
212                                 case SHADER_SOCKET_CLOSURE:
213                                         break;
214                         }
215                 }
216         }
217
218         /* create shader of the appropriate type. we pass "surface" to all shaders,
219          * because "volume" and "displacement" don't work yet in OSL. the shaders
220          * work fine, but presumably these values would be used for more strict
221          * checking, so when that is fixed, we should update the code here too. */
222         if(current_type == SHADER_TYPE_SURFACE)
223                 ss->Shader("surface", name, id(node).c_str());
224         else if(current_type == SHADER_TYPE_VOLUME)
225                 ss->Shader("surface", name, id(node).c_str());
226         else if(current_type == SHADER_TYPE_DISPLACEMENT)
227                 ss->Shader("surface", name, id(node).c_str());
228         else
229                 assert(0);
230         
231         /* link inputs to other nodes */
232         foreach(ShaderInput *input, node->inputs) {
233                 if(input->link) {
234                         if(node_skip_input(node, input))
235                                 continue;
236
237                         /* connect shaders */
238                         string id_from = id(input->link->parent);
239                         string id_to = id(node);
240                         string param_from = compatible_name(input->link->name);
241                         string param_to = compatible_name(input->name);
242
243                         /* avoid name conflict with same input/output socket name */
244                         if(input->link->parent->input(input->link->name))
245                                 param_from += "_";
246
247                         ss->ConnectShaders(id_from.c_str(), param_from.c_str(), id_to.c_str(), param_to.c_str());
248                 }
249         }
250 }
251
252 void OSLCompiler::parameter(const char *name, float f)
253 {
254         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
255         ss->Parameter(name, TypeDesc::TypeFloat, &f);
256 }
257
258 void OSLCompiler::parameter_color(const char *name, float3 f)
259 {
260         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
261         ss->Parameter(name, TypeDesc::TypeColor, &f);
262 }
263
264 void OSLCompiler::parameter_point(const char *name, float3 f)
265 {
266         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
267         ss->Parameter(name, TypeDesc::TypePoint, &f);
268 }
269
270 void OSLCompiler::parameter_normal(const char *name, float3 f)
271 {
272         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
273         ss->Parameter(name, TypeDesc::TypeNormal, &f);
274 }
275
276 void OSLCompiler::parameter_vector(const char *name, float3 f)
277 {
278         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
279         ss->Parameter(name, TypeDesc::TypeVector, &f);
280 }
281
282 void OSLCompiler::parameter(const char *name, int f)
283 {
284         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
285         ss->Parameter(name, TypeDesc::TypeInt, &f);
286 }
287
288 void OSLCompiler::parameter(const char *name, const char *s)
289 {
290         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
291         ss->Parameter(name, TypeDesc::TypeString, &s);
292 }
293
294 void OSLCompiler::parameter(const char *name, ustring s)
295 {
296         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
297         const char *str = s.c_str();
298         ss->Parameter(name, TypeDesc::TypeString, &str);
299 }
300
301 void OSLCompiler::parameter(const char *name, const Transform& tfm)
302 {
303         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
304         ss->Parameter(name, TypeDesc::TypeMatrix, (float*)&tfm);
305 }
306
307 void OSLCompiler::find_dependencies(set<ShaderNode*>& dependencies, ShaderInput *input)
308 {
309         ShaderNode *node = (input->link)? input->link->parent: NULL;
310
311         if(node) {
312                 foreach(ShaderInput *in, node->inputs)
313                         if(!node_skip_input(node, in))
314                                 find_dependencies(dependencies, in);
315
316                 dependencies.insert(node);
317         }
318 }
319
320 void OSLCompiler::generate_nodes(const set<ShaderNode*>& nodes)
321 {
322         set<ShaderNode*> done;
323         bool nodes_done;
324
325         do {
326                 nodes_done = true;
327
328                 foreach(ShaderNode *node, nodes) {
329                         if(done.find(node) == done.end()) {
330                                 bool inputs_done = true;
331
332                                 foreach(ShaderInput *input, node->inputs)
333                                         if(!node_skip_input(node, input))
334                                                 if(input->link && done.find(input->link->parent) == done.end())
335                                                         inputs_done = false;
336
337                                 if(inputs_done) {
338                                         node->compile(*this);
339                                         done.insert(node);
340
341                                         if(node->name == ustring("emission"))
342                                                 current_shader->has_surface_emission = true;
343                                 }
344                                 else
345                                         nodes_done = false;
346                         }
347                 }
348         } while(!nodes_done);
349 }
350
351 void OSLCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
352 {
353         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
354
355         current_type = type;
356
357         ss->ShaderGroupBegin();
358
359         ShaderNode *output = graph->output();
360         set<ShaderNode*> dependencies;
361
362         if(type == SHADER_TYPE_SURFACE) {
363                 /* generate surface shader */
364                 find_dependencies(dependencies, output->input("Surface"));
365                 generate_nodes(dependencies);
366                 output->compile(*this);
367         }
368         else if(type == SHADER_TYPE_VOLUME) {
369                 /* generate volume shader */
370                 find_dependencies(dependencies, output->input("Volume"));
371                 generate_nodes(dependencies);
372                 output->compile(*this);
373         }
374         else if(type == SHADER_TYPE_DISPLACEMENT) {
375                 /* generate displacement shader */
376                 find_dependencies(dependencies, output->input("Displacement"));
377                 generate_nodes(dependencies);
378                 output->compile(*this);
379         }
380         else
381                 assert(0);
382
383         ss->ShaderGroupEnd();
384 }
385
386 void OSLCompiler::compile(OSLGlobals *og, Shader *shader)
387 {
388         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
389         ShaderGraph *graph = shader->graph;
390         ShaderNode *output = (graph)? graph->output(): NULL;
391
392         /* copy graph for shader with bump mapping */
393         if(output->input("Surface")->link && output->input("Displacement")->link)
394                 if(!shader->graph_bump)
395                         shader->graph_bump = shader->graph->copy();
396
397         /* finalize */
398         shader->graph->finalize(false, true);
399         if(shader->graph_bump)
400                 shader->graph_bump->finalize(true, true);
401
402         current_shader = shader;
403
404         shader->has_surface = false;
405         shader->has_surface_emission = false;
406         shader->has_volume = false;
407         shader->has_displacement = false;
408
409         /* generate surface shader */
410         if(graph && output->input("Surface")->link) {
411                 compile_type(shader, shader->graph, SHADER_TYPE_SURFACE);
412                 og->surface_state.push_back(ss->state());
413
414                 if(shader->graph_bump) {
415                         ss->clear_state();
416                         compile_type(shader, shader->graph_bump, SHADER_TYPE_SURFACE);
417                         og->surface_state.push_back(ss->state());
418                 }
419                 else
420                         og->surface_state.push_back(ss->state());
421
422                 ss->clear_state();
423
424                 shader->has_surface = true;
425         }
426         else {
427                 og->surface_state.push_back(OSL::ShadingAttribStateRef());
428                 og->surface_state.push_back(OSL::ShadingAttribStateRef());
429         }
430
431         /* generate volume shader */
432         if(graph && output->input("Volume")->link) {
433                 compile_type(shader, shader->graph, SHADER_TYPE_VOLUME);
434                 shader->has_volume = true;
435
436                 og->volume_state.push_back(ss->state());
437                 og->volume_state.push_back(ss->state());
438                 ss->clear_state();
439         }
440         else {
441                 og->volume_state.push_back(OSL::ShadingAttribStateRef());
442                 og->volume_state.push_back(OSL::ShadingAttribStateRef());
443         }
444
445         /* generate displacement shader */
446         if(graph && output->input("Displacement")->link) {
447                 compile_type(shader, shader->graph, SHADER_TYPE_DISPLACEMENT);
448                 shader->has_displacement = true;
449
450                 og->displacement_state.push_back(ss->state());
451                 og->displacement_state.push_back(ss->state());
452                 ss->clear_state();
453         }
454         else {
455                 og->displacement_state.push_back(OSL::ShadingAttribStateRef());
456                 og->displacement_state.push_back(OSL::ShadingAttribStateRef());
457         }
458 }
459
460 #else
461
462 void OSLCompiler::add(ShaderNode *node, const char *name)
463 {
464 }
465
466 void OSLCompiler::parameter(const char *name, float f)
467 {
468 }
469
470 void OSLCompiler::parameter_color(const char *name, float3 f)
471 {
472 }
473
474 void OSLCompiler::parameter_vector(const char *name, float3 f)
475 {
476 }
477
478 void OSLCompiler::parameter(const char *name, int f)
479 {
480 }
481
482 void OSLCompiler::parameter(const char *name, const char *s)
483 {
484 }
485
486 void OSLCompiler::parameter(const char *name, ustring s)
487 {
488 }
489
490 void OSLCompiler::parameter(const char *name, const Transform& tfm)
491 {
492 }
493
494 #endif /* WITH_OSL */
495
496 CCL_NAMESPACE_END
497