2 * Copyright 2011, Blender Foundation.
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.
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.
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.
29 #include "osl_globals.h"
30 #include "osl_services.h"
31 #include "osl_shader.h"
33 #include "util_foreach.h"
34 #include "util_path.h"
35 #include "util_progress.h"
45 OSLShaderManager::OSLShaderManager()
47 services = new OSLRenderServices();
49 /* if we let OSL create it, it leaks */
50 ts = TextureSystem::create(true);
51 ts->attribute("automip", 1);
52 ts->attribute("autotile", 64);
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());
62 OSLShader::register_closures(ss);
65 OSLShaderManager::~OSLShaderManager()
67 OSL::ShadingSystem::destroy(ss);
68 OSL::TextureSystem::destroy(ts);
72 void OSLShaderManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
74 /* test if we need to update */
75 bool need_update = false;
77 foreach(Shader *shader, scene->shaders)
78 if(shader->need_update)
84 device_free(device, dscene);
87 OSLGlobals *og = (OSLGlobals*)device->osl_memory();
89 foreach(Shader *shader, scene->shaders) {
90 assert(shader->graph);
92 if(progress.get_cancel()) return;
94 if(shader->sample_as_light && shader->has_surface_emission)
95 scene->light_manager->need_update = true;
97 OSLCompiler compiler((void*)ss);
98 compiler.background = (shader == scene->shaders[scene->default_background]);
99 compiler.compile(og, shader);
102 /* setup shader engine */
104 int background_id = scene->shader_manager->get_shader_id(scene->default_background);
105 og->background_state = og->surface_state[background_id];
108 tls_create(OSLGlobals::ThreadData, og->thread_data);
110 foreach(Shader *shader, scene->shaders)
111 shader->need_update = false;
113 /* set texture system */
114 scene->image_manager->set_osl_texture_system((void*)ts);
116 device_update_common(device, dscene, scene, progress);
119 void OSLShaderManager::device_free(Device *device, DeviceScene *dscene)
121 OSLGlobals *og = (OSLGlobals*)device->osl_memory();
123 device_free_common(device, dscene);
125 /* clear shader engine */
129 tls_delete(OSLGlobals::ThreadData, og->thread_data);
131 og->surface_state.clear();
132 og->volume_state.clear();
133 og->displacement_state.clear();
134 og->background_state.reset();
139 OSLCompiler::OSLCompiler(void *shadingsys_)
141 shadingsys = shadingsys_;
142 current_type = SHADER_TYPE_SURFACE;
143 current_shader = NULL;
147 string OSLCompiler::id(ShaderNode *node)
149 /* assign layer unique name based on pointer address + bump mode */
151 stream << "node_" << node->name << "_" << node;
156 string OSLCompiler::compatible_name(const char *name)
161 while((i = sname.find(" ")) != string::npos)
162 sname.replace(i, 1, "");
167 bool OSLCompiler::node_skip_input(ShaderNode *node, ShaderInput *input)
169 /* exception for output node, only one input is actually used
170 depending on the current shader type */
172 if(node->name == ustring("output")) {
173 if(strcmp(input->name, "Surface") == 0 && current_type != SHADER_TYPE_SURFACE)
175 if(strcmp(input->name, "Volume") == 0 && current_type != SHADER_TYPE_VOLUME)
177 if(strcmp(input->name, "Displacement") == 0 && current_type != SHADER_TYPE_DISPLACEMENT)
180 else if(current_type == SHADER_TYPE_DISPLACEMENT && input->link && input->link->parent->name == ustring("bump"))
186 void OSLCompiler::add(ShaderNode *node, const char *name)
188 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
190 /* pass in fixed parameter values */
191 foreach(ShaderInput *input, node->inputs) {
193 /* checks to untangle graphs */
194 if(node_skip_input(node, input))
196 /* already has default value assigned */
197 else if(input->default_value != ShaderInput::NONE)
200 switch(input->type) {
201 case SHADER_SOCKET_COLOR:
202 parameter_color(input->name, input->value);
204 case SHADER_SOCKET_POINT:
205 parameter_point(input->name, input->value);
207 case SHADER_SOCKET_VECTOR:
208 parameter_vector(input->name, input->value);
210 case SHADER_SOCKET_NORMAL:
211 parameter_normal(input->name, input->value);
213 case SHADER_SOCKET_FLOAT:
214 parameter(input->name, input->value.x);
216 case SHADER_SOCKET_CLOSURE:
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());
235 /* link inputs to other nodes */
236 foreach(ShaderInput *input, node->inputs) {
238 if(node_skip_input(node, input))
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);
247 /* avoid name conflict with same input/output socket name */
248 if(input->link->parent->input(input->link->name))
251 ss->ConnectShaders(id_from.c_str(), param_from.c_str(), id_to.c_str(), param_to.c_str());
256 void OSLCompiler::parameter(const char *name, float f)
258 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
259 ss->Parameter(name, TypeDesc::TypeFloat, &f);
262 void OSLCompiler::parameter_color(const char *name, float3 f)
264 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
265 ss->Parameter(name, TypeDesc::TypeColor, &f);
268 void OSLCompiler::parameter_point(const char *name, float3 f)
270 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
271 ss->Parameter(name, TypeDesc::TypePoint, &f);
274 void OSLCompiler::parameter_normal(const char *name, float3 f)
276 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
277 ss->Parameter(name, TypeDesc::TypeNormal, &f);
280 void OSLCompiler::parameter_vector(const char *name, float3 f)
282 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
283 ss->Parameter(name, TypeDesc::TypeVector, &f);
286 void OSLCompiler::parameter(const char *name, int f)
288 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
289 ss->Parameter(name, TypeDesc::TypeInt, &f);
292 void OSLCompiler::parameter(const char *name, const char *s)
294 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
295 ss->Parameter(name, TypeDesc::TypeString, &s);
298 void OSLCompiler::parameter(const char *name, ustring s)
300 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
301 const char *str = s.c_str();
302 ss->Parameter(name, TypeDesc::TypeString, &str);
305 void OSLCompiler::parameter(const char *name, const Transform& tfm)
307 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
308 ss->Parameter(name, TypeDesc::TypeMatrix, (float*)&tfm);
311 void OSLCompiler::find_dependencies(set<ShaderNode*>& dependencies, ShaderInput *input)
313 ShaderNode *node = (input->link)? input->link->parent: NULL;
316 foreach(ShaderInput *in, node->inputs)
317 if(!node_skip_input(node, in))
318 find_dependencies(dependencies, in);
320 dependencies.insert(node);
324 void OSLCompiler::generate_nodes(const set<ShaderNode*>& nodes)
326 set<ShaderNode*> done;
332 foreach(ShaderNode *node, nodes) {
333 if(done.find(node) == done.end()) {
334 bool inputs_done = true;
336 foreach(ShaderInput *input, node->inputs)
337 if(!node_skip_input(node, input))
338 if(input->link && done.find(input->link->parent) == done.end())
342 node->compile(*this);
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;
354 } while(!nodes_done);
357 void OSLCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
359 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
363 ss->ShaderGroupBegin();
365 ShaderNode *output = graph->output();
366 set<ShaderNode*> dependencies;
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);
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);
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);
389 ss->ShaderGroupEnd();
392 void OSLCompiler::compile(OSLGlobals *og, Shader *shader)
394 OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
395 ShaderGraph *graph = shader->graph;
396 ShaderNode *output = (graph)? graph->output(): NULL;
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();
404 shader->graph->finalize(false, true);
405 if(shader->graph_bump)
406 shader->graph_bump->finalize(true, true);
408 current_shader = shader;
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;
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());
421 if(shader->graph_bump) {
423 compile_type(shader, shader->graph_bump, SHADER_TYPE_SURFACE);
424 og->surface_state.push_back(ss->state());
427 og->surface_state.push_back(ss->state());
431 shader->has_surface = true;
434 og->surface_state.push_back(OSL::ShadingAttribStateRef());
435 og->surface_state.push_back(OSL::ShadingAttribStateRef());
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;
443 og->volume_state.push_back(ss->state());
444 og->volume_state.push_back(ss->state());
448 og->volume_state.push_back(OSL::ShadingAttribStateRef());
449 og->volume_state.push_back(OSL::ShadingAttribStateRef());
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;
457 og->displacement_state.push_back(ss->state());
458 og->displacement_state.push_back(ss->state());
462 og->displacement_state.push_back(OSL::ShadingAttribStateRef());
463 og->displacement_state.push_back(OSL::ShadingAttribStateRef());
469 void OSLCompiler::add(ShaderNode *node, const char *name)
473 void OSLCompiler::parameter(const char *name, float f)
477 void OSLCompiler::parameter_color(const char *name, float3 f)
481 void OSLCompiler::parameter_vector(const char *name, float3 f)
485 void OSLCompiler::parameter(const char *name, int f)
489 void OSLCompiler::parameter(const char *name, const char *s)
493 void OSLCompiler::parameter(const char *name, ustring s)
497 void OSLCompiler::parameter(const char *name, const Transform& tfm)
501 #endif /* WITH_OSL */