Merging r57518 through r57545 from trunk into soc-20133-depsgraph_mt
[blender-staging.git] / intern / cycles / render / shader.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 "bssrdf.h"
20 #include "device.h"
21 #include "graph.h"
22 #include "light.h"
23 #include "mesh.h"
24 #include "nodes.h"
25 #include "osl.h"
26 #include "scene.h"
27 #include "shader.h"
28 #include "svm.h"
29 #include "tables.h"
30
31 #include "util_foreach.h"
32
33 CCL_NAMESPACE_BEGIN
34
35 /* Shader */
36
37 Shader::Shader()
38 {
39         name = "";
40         pass_id = 0;
41
42         graph = NULL;
43         graph_bump = NULL;
44
45         use_mis = true;
46         use_transparent_shadow = true;
47         homogeneous_volume = false;
48
49         has_surface = false;
50         has_surface_transparent = false;
51         has_surface_emission = false;
52         has_surface_bssrdf = false;
53         has_volume = false;
54         has_displacement = false;
55
56         used = false;
57
58         need_update = true;
59         need_update_attributes = true;
60 }
61
62 Shader::~Shader()
63 {
64         delete graph;
65         delete graph_bump;
66 }
67
68 void Shader::set_graph(ShaderGraph *graph_)
69 {
70         /* do this here already so that we can detect if mesh or object attributes
71          * are needed, since the node attribute callbacks check if their sockets
72          * are connected but proxy nodes should not count */
73         if(graph_)
74                 graph_->remove_unneeded_nodes();
75
76         /* assign graph */
77         delete graph;
78         delete graph_bump;
79         graph = graph_;
80         graph_bump = NULL;
81 }
82
83 void Shader::tag_update(Scene *scene)
84 {
85         /* update tag */
86         need_update = true;
87         scene->shader_manager->need_update = true;
88
89         /* if the shader previously was emissive, update light distribution,
90          * if the new shader is emissive, a light manager update tag will be
91          * done in the shader manager device update. */
92         if(use_mis && has_surface_emission)
93                 scene->light_manager->need_update = true;
94
95         /* get requested attributes. this could be optimized by pruning unused
96          * nodes here already, but that's the job of the shader manager currently,
97          * and may not be so great for interactive rendering where you temporarily
98          * disconnect a node */
99         AttributeRequestSet prev_attributes = attributes;
100
101         attributes.clear();
102         foreach(ShaderNode *node, graph->nodes)
103                 node->attributes(&attributes);
104         
105         /* compare if the attributes changed, mesh manager will check
106          * need_update_attributes, update the relevant meshes and clear it. */
107         if(attributes.modified(prev_attributes)) {
108                 need_update_attributes = true;
109                 scene->mesh_manager->need_update = true;
110         }
111 }
112
113 void Shader::tag_used(Scene *scene)
114 {
115         /* if an unused shader suddenly gets used somewhere, it needs to be
116          * recompiled because it was skipped for compilation before */
117         if(!used) {
118                 need_update = true;
119                 scene->shader_manager->need_update = true;
120         }
121 }
122
123 /* Shader Manager */
124
125 ShaderManager::ShaderManager()
126 {
127         need_update = true;
128         bssrdf_table_offset = TABLE_OFFSET_INVALID;
129 }
130
131 ShaderManager::~ShaderManager()
132 {
133 }
134
135 ShaderManager *ShaderManager::create(Scene *scene, int shadingsystem)
136 {
137         ShaderManager *manager;
138
139 #ifdef WITH_OSL
140         if(shadingsystem == SceneParams::OSL)
141                 manager = new OSLShaderManager();
142         else
143 #endif
144                 manager = new SVMShaderManager();
145         
146         add_default(scene);
147
148         return manager;
149 }
150
151 uint ShaderManager::get_attribute_id(ustring name)
152 {
153         /* get a unique id for each name, for SVM attribute lookup */
154         AttributeIDMap::iterator it = unique_attribute_id.find(name);
155
156         if(it != unique_attribute_id.end())
157                 return it->second;
158         
159         uint id = (uint)ATTR_STD_NUM + unique_attribute_id.size();
160         unique_attribute_id[name] = id;
161         return id;
162 }
163
164 uint ShaderManager::get_attribute_id(AttributeStandard std)
165 {
166         return (uint)std;
167 }
168
169 int ShaderManager::get_shader_id(uint shader, Mesh *mesh, bool smooth)
170 {
171         /* get a shader id to pass to the kernel */
172         int id = shader*2;
173         
174         /* index depends bump since this setting is not in the shader */
175         if(mesh && mesh->displacement_method != Mesh::DISPLACE_TRUE)
176                 id += 1;
177         /* smooth flag */
178         if(smooth)
179                 id |= SHADER_SMOOTH_NORMAL;
180         
181         /* default flags */
182         id |= SHADER_CAST_SHADOW|SHADER_AREA_LIGHT;
183         
184         return id;
185 }
186
187 void ShaderManager::device_update_shaders_used(Scene *scene)
188 {
189         /* figure out which shaders are in use, so SVM/OSL can skip compiling them
190          * for speed and avoid loading image textures into memory */
191         foreach(Shader *shader, scene->shaders)
192                 shader->used = false;
193
194         scene->shaders[scene->default_surface]->used = true;
195         scene->shaders[scene->default_light]->used = true;
196         scene->shaders[scene->default_background]->used = true;
197         scene->shaders[scene->default_holdout]->used = true;
198         scene->shaders[scene->default_empty]->used = true;
199
200         foreach(Mesh *mesh, scene->meshes)
201                 foreach(uint shader, mesh->used_shaders)
202                         scene->shaders[shader]->used = true;
203
204         foreach(Light *light, scene->lights)
205                 scene->shaders[light->shader]->used = true;
206 }
207
208 void ShaderManager::device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
209 {
210         device->tex_free(dscene->shader_flag);
211         dscene->shader_flag.clear();
212
213         if(scene->shaders.size() == 0)
214                 return;
215
216         uint shader_flag_size = scene->shaders.size()*4;
217         uint *shader_flag = dscene->shader_flag.resize(shader_flag_size);
218         uint i = 0;
219         bool has_surface_bssrdf = false;
220
221         foreach(Shader *shader, scene->shaders) {
222                 uint flag = 0;
223
224                 if(shader->use_mis)
225                         flag |= SD_USE_MIS;
226                 if(shader->has_surface_transparent && shader->use_transparent_shadow)
227                         flag |= SD_HAS_TRANSPARENT_SHADOW;
228                 if(shader->has_volume)
229                         flag |= SD_HAS_VOLUME;
230                 if(shader->homogeneous_volume)
231                         flag |= SD_HOMOGENEOUS_VOLUME;
232                 if(shader->has_surface_bssrdf)
233                         has_surface_bssrdf = true;
234
235                 shader_flag[i++] = flag;
236                 shader_flag[i++] = shader->pass_id;
237                 shader_flag[i++] = flag;
238                 shader_flag[i++] = shader->pass_id;
239         }
240
241         device->tex_alloc("__shader_flag", dscene->shader_flag);
242
243         /* bssrdf lookup table */
244         KernelBSSRDF *kbssrdf = &dscene->data.bssrdf;
245
246         if(has_surface_bssrdf && bssrdf_table_offset == TABLE_OFFSET_INVALID) {
247                 vector<float> table;
248
249                 bssrdf_table_build(table);
250                 bssrdf_table_offset = scene->lookup_tables->add_table(dscene, table);
251
252                 kbssrdf->table_offset = (int)bssrdf_table_offset;
253                 kbssrdf->num_attempts = BSSRDF_MAX_ATTEMPTS;
254         }
255         else if(!has_surface_bssrdf && bssrdf_table_offset != TABLE_OFFSET_INVALID) {
256                 scene->lookup_tables->remove_table(bssrdf_table_offset);
257                 bssrdf_table_offset = TABLE_OFFSET_INVALID;
258         }
259 }
260
261 void ShaderManager::device_free_common(Device *device, DeviceScene *dscene, Scene *scene)
262 {
263         if(bssrdf_table_offset != TABLE_OFFSET_INVALID) {
264                 scene->lookup_tables->remove_table(bssrdf_table_offset);
265                 bssrdf_table_offset = TABLE_OFFSET_INVALID;
266         }
267
268         device->tex_free(dscene->shader_flag);
269         dscene->shader_flag.clear();
270 }
271
272 void ShaderManager::add_default(Scene *scene)
273 {
274         Shader *shader;
275         ShaderGraph *graph;
276         ShaderNode *closure, *out;
277
278         /* default surface */
279         {
280                 graph = new ShaderGraph();
281
282                 closure = graph->add(new DiffuseBsdfNode());
283                 closure->input("Color")->value = make_float3(0.8f, 0.8f, 0.8f);
284                 out = graph->output();
285
286                 graph->connect(closure->output("BSDF"), out->input("Surface"));
287
288                 shader = new Shader();
289                 shader->name = "default_surface";
290                 shader->graph = graph;
291                 scene->shaders.push_back(shader);
292                 scene->default_surface = scene->shaders.size() - 1;
293         }
294
295         /* default light */
296         {
297                 graph = new ShaderGraph();
298
299                 closure = graph->add(new EmissionNode());
300                 closure->input("Color")->value = make_float3(0.8f, 0.8f, 0.8f);
301                 closure->input("Strength")->value.x = 0.0f;
302                 out = graph->output();
303
304                 graph->connect(closure->output("Emission"), out->input("Surface"));
305
306                 shader = new Shader();
307                 shader->name = "default_light";
308                 shader->graph = graph;
309                 scene->shaders.push_back(shader);
310                 scene->default_light = scene->shaders.size() - 1;
311         }
312
313         /* default background */
314         {
315                 graph = new ShaderGraph();
316
317                 closure = graph->add(new BackgroundNode());
318                 closure->input("Color")->value = make_float3(0.8f, 0.8f, 0.8f);
319                 out = graph->output();
320
321                 graph->connect(closure->output("Background"), out->input("Surface"));
322
323                 shader = new Shader();
324                 shader->name = "default_background";
325                 shader->graph = graph;
326                 scene->shaders.push_back(shader);
327                 scene->default_background = scene->shaders.size() - 1;
328         }
329
330         /* default holdout */
331         {
332                 graph = new ShaderGraph();
333
334                 closure = graph->add(new HoldoutNode());
335                 out = graph->output();
336
337                 graph->connect(closure->output("Holdout"), out->input("Surface"));
338
339                 shader = new Shader();
340                 shader->name = "default_holdout";
341                 shader->graph = graph;
342                 scene->shaders.push_back(shader);
343                 scene->default_holdout = scene->shaders.size() - 1;
344         }
345
346         /* default empty */
347         {
348                 graph = new ShaderGraph();
349
350                 shader = new Shader();
351                 shader->name = "default_empty";
352                 shader->graph = graph;
353                 scene->shaders.push_back(shader);
354                 scene->default_empty = scene->shaders.size() - 1;
355         }
356 }
357
358 CCL_NAMESPACE_END
359