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