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