492529c30a2f33b5ac3b320ec024ce04f0c68e82
[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->sample_as_light && 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->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         device_update_common(device, dscene, scene, progress);
117 }
118
119 void OSLShaderManager::device_free(Device *device, DeviceScene *dscene)
120 {
121         OSLGlobals *og = (OSLGlobals*)device->osl_memory();
122
123         device_free_common(device, dscene);
124
125         /* clear shader engine */
126         og->use = false;
127         og->ss = NULL;
128
129         tls_delete(OSLGlobals::ThreadData, og->thread_data);
130
131         og->state.clear();
132         og->displacement_state.clear();
133         og->background_state.reset();
134 }
135
136 /* Graph Compiler */
137
138 OSLCompiler::OSLCompiler(void *shadingsys_)
139 {
140         shadingsys = shadingsys_;
141         current_type = SHADER_TYPE_CLOSURE;
142         current_shader = NULL;
143         background = false;
144 }
145
146 string OSLCompiler::id(ShaderNode *node)
147 {
148         /* assign layer unique name based on pointer address + bump mode */
149         stringstream stream;
150         stream << "node_" << node->name << "_" << node;
151
152         return stream.str();
153 }
154
155 string OSLCompiler::compatible_name(const char *name)
156 {
157         string sname = name;
158         size_t i;
159
160         while((i = sname.find(" ")) != string::npos)
161                 sname.replace(i, 1, "");
162         
163         return sname;
164 }
165
166 bool OSLCompiler::node_skip_input(ShaderNode *node, ShaderInput *input)
167 {
168         /* exception for output node, only one input is actually used
169            depending on the current shader type */
170
171         if(node->name == ustring("output")) {
172                 if(strcmp(input->name, "Closure") == 0 && current_type != SHADER_TYPE_CLOSURE)
173                         return true;
174                 if(strcmp(input->name, "Displacement") == 0 && current_type != SHADER_TYPE_DISPLACEMENT)
175                         return true;
176         }
177         else if(current_type == SHADER_TYPE_DISPLACEMENT && input->link && input->link->parent->name == ustring("bump"))
178                 return true;
179
180         return false;
181 }
182
183 void OSLCompiler::add(ShaderNode *node, const char *name)
184 {
185         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
186
187         /* pass in fixed parameter values */
188         foreach(ShaderInput *input, node->inputs) {
189                 if(!input->link) {
190                         /* checks to untangle graphs */
191                         if(node_skip_input(node, input))
192                                 continue;
193                         /* already has default value assigned */
194                         else if(input->default_value != ShaderInput::NONE)
195                                 continue;
196
197                         switch(input->type) {
198                                 case SHADER_SOCKET_COLOR:
199                                         parameter_color(input->name, input->value);
200                                         break;
201                                 case SHADER_SOCKET_POINT:
202                                         parameter_point(input->name, input->value);
203                                         break;
204                                 case SHADER_SOCKET_VECTOR:
205                                         parameter_vector(input->name, input->value);
206                                         break;
207                                 case SHADER_SOCKET_NORMAL:
208                                         parameter_normal(input->name, input->value);
209                                         break;
210                                 case SHADER_SOCKET_FLOAT:
211                                         parameter(input->name, input->value.x);
212                                         break;
213                                 case SHADER_SOCKET_CLOSURE:
214                                         break;
215                         }
216                 }
217         }
218
219         /* create shader of the appropriate type. we pass "surface" to all shaders,
220          * because "volume" and "displacement" don't work yet in OSL. the shaders
221          * work fine, but presumably these values would be used for more strict
222          * checking, so when that is fixed, we should update the code here too. */
223         if(current_type == SHADER_TYPE_CLOSURE)
224                 ss->Shader("surface", name, id(node).c_str());
225         else if(current_type == SHADER_TYPE_DISPLACEMENT)
226                 ss->Shader("surface", name, id(node).c_str());
227         else
228                 assert(0);
229         
230         /* link inputs to other nodes */
231         foreach(ShaderInput *input, node->inputs) {
232                 if(input->link) {
233                         if(node_skip_input(node, input))
234                                 continue;
235
236                         /* connect shaders */
237                         string id_from = id(input->link->parent);
238                         string id_to = id(node);
239                         string param_from = compatible_name(input->link->name);
240                         string param_to = compatible_name(input->name);
241
242                         /* avoid name conflict with same input/output socket name */
243                         if(input->link->parent->input(input->link->name))
244                                 param_from += "_";
245
246                         ss->ConnectShaders(id_from.c_str(), param_from.c_str(), id_to.c_str(), param_to.c_str());
247                 }
248         }
249 }
250
251 void OSLCompiler::parameter(const char *name, float f)
252 {
253         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
254         ss->Parameter(name, TypeDesc::TypeFloat, &f);
255 }
256
257 void OSLCompiler::parameter_color(const char *name, float3 f)
258 {
259         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
260         ss->Parameter(name, TypeDesc::TypeColor, &f);
261 }
262
263 void OSLCompiler::parameter_point(const char *name, float3 f)
264 {
265         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
266         ss->Parameter(name, TypeDesc::TypePoint, &f);
267 }
268
269 void OSLCompiler::parameter_normal(const char *name, float3 f)
270 {
271         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
272         ss->Parameter(name, TypeDesc::TypeNormal, &f);
273 }
274
275 void OSLCompiler::parameter_vector(const char *name, float3 f)
276 {
277         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
278         ss->Parameter(name, TypeDesc::TypeVector, &f);
279 }
280
281 void OSLCompiler::parameter(const char *name, int f)
282 {
283         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
284         ss->Parameter(name, TypeDesc::TypeInt, &f);
285 }
286
287 void OSLCompiler::parameter(const char *name, const char *s)
288 {
289         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
290         ss->Parameter(name, TypeDesc::TypeString, &s);
291 }
292
293 void OSLCompiler::parameter(const char *name, ustring s)
294 {
295         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
296         const char *str = s.c_str();
297         ss->Parameter(name, TypeDesc::TypeString, &str);
298 }
299
300 void OSLCompiler::parameter(const char *name, const Transform& tfm)
301 {
302         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
303         ss->Parameter(name, TypeDesc::TypeMatrix, (float*)&tfm);
304 }
305
306 void OSLCompiler::find_dependencies(set<ShaderNode*>& dependencies, ShaderInput *input)
307 {
308         ShaderNode *node = (input->link)? input->link->parent: NULL;
309
310         if(node) {
311                 foreach(ShaderInput *in, node->inputs)
312                         if(!node_skip_input(node, in))
313                                 find_dependencies(dependencies, in);
314
315                 dependencies.insert(node);
316         }
317 }
318
319 void OSLCompiler::generate_nodes(const set<ShaderNode*>& nodes)
320 {
321         set<ShaderNode*> done;
322         bool nodes_done;
323
324         do {
325                 nodes_done = true;
326
327                 foreach(ShaderNode *node, nodes) {
328                         if(done.find(node) == done.end()) {
329                                 bool inputs_done = true;
330
331                                 foreach(ShaderInput *input, node->inputs)
332                                         if(!node_skip_input(node, input))
333                                                 if(input->link && done.find(input->link->parent) == done.end())
334                                                         inputs_done = false;
335
336                                 if(inputs_done) {
337                                         node->compile(*this);
338                                         done.insert(node);
339
340                                         if(node->name == ustring("emission"))
341                                                 current_shader->has_surface_emission = true;
342                                         if(node->name == ustring("transparent"))
343                                                 current_shader->has_surface_transparent = true;
344                                         if(node->name == ustring("volume"))
345                                                 current_shader->has_volume = true;
346                                 }
347                                 else
348                                         nodes_done = false;
349                         }
350                 }
351         } while(!nodes_done);
352 }
353
354 void OSLCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
355 {
356         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
357
358         current_type = type;
359
360         ss->ShaderGroupBegin();
361
362         ShaderNode *output = graph->output();
363         set<ShaderNode*> dependencies;
364
365         if(type == SHADER_TYPE_CLOSURE) {
366                 /* generate surface shader */
367                 find_dependencies(dependencies, output->input("Closure"));
368                 generate_nodes(dependencies);
369                 output->compile(*this);
370         }
371         else if(type == SHADER_TYPE_DISPLACEMENT) {
372                 /* generate displacement shader */
373                 find_dependencies(dependencies, output->input("Displacement"));
374                 generate_nodes(dependencies);
375                 output->compile(*this);
376         }
377         else
378                 assert(0);
379
380         ss->ShaderGroupEnd();
381 }
382
383 void OSLCompiler::compile(OSLGlobals *og, Shader *shader)
384 {
385         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
386         ShaderGraph *graph = shader->graph;
387         ShaderNode *output = (graph)? graph->output(): NULL;
388
389         /* copy graph for shader with bump mapping */
390         if(output->input("Closure")->link && output->input("Displacement")->link)
391                 if(!shader->graph_bump)
392                         shader->graph_bump = shader->graph->copy();
393
394         /* finalize */
395         shader->graph->finalize(false, true);
396         if(shader->graph_bump)
397                 shader->graph_bump->finalize(true, true);
398
399         current_shader = shader;
400
401         shader->has_surface_emission = false;
402         shader->has_surface_transparent = false;
403         shader->has_volume = false;
404         shader->has_displacement = false;
405
406         /* generate surface shader */
407         if(graph && output->input("Closure")->link) {
408                 compile_type(shader, shader->graph, SHADER_TYPE_CLOSURE);
409                 og->state.push_back(ss->state());
410
411                 if(shader->graph_bump) {
412                         ss->clear_state();
413                         compile_type(shader, shader->graph_bump, SHADER_TYPE_CLOSURE);
414                         og->state.push_back(ss->state());
415                 }
416                 else
417                         og->state.push_back(ss->state());
418
419                 ss->clear_state();
420         }
421         else {
422                 og->state.push_back(OSL::ShadingAttribStateRef());
423                 og->state.push_back(OSL::ShadingAttribStateRef());
424         }
425
426         /* generate displacement shader */
427         if(graph && output->input("Displacement")->link) {
428                 compile_type(shader, shader->graph, SHADER_TYPE_DISPLACEMENT);
429                 shader->has_displacement = true;
430
431                 og->displacement_state.push_back(ss->state());
432                 og->displacement_state.push_back(ss->state());
433                 ss->clear_state();
434         }
435         else {
436                 og->displacement_state.push_back(OSL::ShadingAttribStateRef());
437                 og->displacement_state.push_back(OSL::ShadingAttribStateRef());
438         }
439 }
440
441 #else
442
443 void OSLCompiler::add(ShaderNode *node, const char *name)
444 {
445 }
446
447 void OSLCompiler::parameter(const char *name, float f)
448 {
449 }
450
451 void OSLCompiler::parameter_color(const char *name, float3 f)
452 {
453 }
454
455 void OSLCompiler::parameter_vector(const char *name, float3 f)
456 {
457 }
458
459 void OSLCompiler::parameter(const char *name, int f)
460 {
461 }
462
463 void OSLCompiler::parameter(const char *name, const char *s)
464 {
465 }
466
467 void OSLCompiler::parameter(const char *name, ustring s)
468 {
469 }
470
471 void OSLCompiler::parameter(const char *name, const Transform& tfm)
472 {
473 }
474
475 #endif /* WITH_OSL */
476
477 CCL_NAMESPACE_END
478