4cdb878df45df8572bd0ae72d3c6c34500bb28f3
[blender.git] / intern / cycles / render / shader.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "background.h"
18 #include "camera.h"
19 #include "device.h"
20 #include "graph.h"
21 #include "integrator.h"
22 #include "light.h"
23 #include "mesh.h"
24 #include "nodes.h"
25 #include "object.h"
26 #include "osl.h"
27 #include "scene.h"
28 #include "shader.h"
29 #include "svm.h"
30 #include "tables.h"
31
32 #include "util_foreach.h"
33
34 CCL_NAMESPACE_BEGIN
35
36 vector<float> ShaderManager::beckmann_table;
37
38 /* Beckmann sampling precomputed table, see bsdf_microfacet.h */
39
40 /* 2D slope distribution (alpha = 1.0) */
41 static float beckmann_table_P22(const float slope_x, const float slope_y)
42 {
43         return expf(-(slope_x*slope_x + slope_y*slope_y));
44 }
45
46 /* maximal slope amplitude (range that contains 99.99% of the distribution) */
47 static float beckmann_table_slope_max()
48 {
49         return 6.0;
50 }
51
52 /* Paper used: Importance Sampling Microfacet-Based BSDFs with the
53  * Distribution of Visible Normals. Supplemental Material 2/2.
54  *
55  * http://hal.inria.fr/docs/01/00/66/20/ANNEX/supplemental2.pdf
56  */
57 static void beckmann_table_rows(float *table, int row_from, int row_to)
58 {
59         /* allocate temporary data */
60         const int DATA_TMP_SIZE = 512;
61         vector<double> slope_x(DATA_TMP_SIZE);
62         vector<double> CDF_P22_omega_i(DATA_TMP_SIZE);
63
64         /* loop over incident directions */
65         for(int index_theta = row_from; index_theta < row_to; index_theta++) {
66                 /* incident vector */
67                 const float cos_theta = index_theta / (BECKMANN_TABLE_SIZE - 1.0f);
68                 const float sin_theta = safe_sqrtf(1.0f - cos_theta*cos_theta);
69
70                 /* for a given incident vector
71                  * integrate P22_{omega_i}(x_slope, 1, 1), Eq. (10) */
72                 slope_x[0] = (double)-beckmann_table_slope_max();
73                 CDF_P22_omega_i[0] = 0;
74
75                 for(int index_slope_x = 1; index_slope_x < DATA_TMP_SIZE; ++index_slope_x) {
76                         /* slope_x */
77                         slope_x[index_slope_x] = (double)(-beckmann_table_slope_max() + 2.0f * beckmann_table_slope_max() * index_slope_x/(DATA_TMP_SIZE - 1.0f));
78
79                         /* dot product with incident vector */
80                         float dot_product = fmaxf(0.0f, -(float)slope_x[index_slope_x]*sin_theta + cos_theta);
81                         /* marginalize P22_{omega_i}(x_slope, 1, 1), Eq. (10) */
82                         float P22_omega_i = 0.0f;
83
84                         for(int j = 0; j < 100; ++j) {
85                                 float slope_y = -beckmann_table_slope_max() + 2.0f * beckmann_table_slope_max() * j * (1.0f/99.0f);
86                                 P22_omega_i += dot_product * beckmann_table_P22((float)slope_x[index_slope_x], slope_y);
87                         }
88
89                         /* CDF of P22_{omega_i}(x_slope, 1, 1), Eq. (10) */
90                         CDF_P22_omega_i[index_slope_x] = CDF_P22_omega_i[index_slope_x - 1] + (double)P22_omega_i;
91                 }
92
93                 /* renormalize CDF_P22_omega_i */
94                 for(int index_slope_x = 1; index_slope_x < DATA_TMP_SIZE; ++index_slope_x)
95                         CDF_P22_omega_i[index_slope_x] /= CDF_P22_omega_i[DATA_TMP_SIZE - 1];
96
97                 /* loop over random number U1 */
98                 int index_slope_x = 0;
99
100                 for(int index_U = 0; index_U < BECKMANN_TABLE_SIZE; ++index_U) {
101                         const double U = 0.0000001 + 0.9999998 * index_U / (double)(BECKMANN_TABLE_SIZE - 1);
102
103                         /* inverse CDF_P22_omega_i, solve Eq.(11) */
104                         while(CDF_P22_omega_i[index_slope_x] <= U)
105                                 ++index_slope_x;
106
107                         const double interp =
108                                 (CDF_P22_omega_i[index_slope_x] - U) /
109                                 (CDF_P22_omega_i[index_slope_x] - CDF_P22_omega_i[index_slope_x - 1]);
110
111                         /* store value */
112                         table[index_U + index_theta*BECKMANN_TABLE_SIZE] = (float)(
113                                 interp * slope_x[index_slope_x - 1] +
114                                     (1.0 - interp) * slope_x[index_slope_x]);
115                 }
116         }
117 }
118
119 static void beckmann_table_build(vector<float>& table)
120 {
121         table.resize(BECKMANN_TABLE_SIZE*BECKMANN_TABLE_SIZE);
122
123         /* multithreaded build */
124         TaskPool pool;
125
126         for(int i = 0; i < BECKMANN_TABLE_SIZE; i+=8)
127                 pool.push(function_bind(&beckmann_table_rows, &table[0], i, i+8));
128
129         pool.wait_work();
130 }
131
132 /* Shader */
133
134 NODE_DEFINE(Shader)
135 {
136         NodeType* type = NodeType::add("shader", create);
137
138         SOCKET_BOOLEAN(use_mis, "Use MIS", true);
139         SOCKET_BOOLEAN(use_transparent_shadow, "Use Transparent Shadow", true);
140         SOCKET_BOOLEAN(heterogeneous_volume, "Heterogeneous Volume", true);
141
142         static NodeEnum volume_sampling_method_enum;
143         volume_sampling_method_enum.insert("distance", VOLUME_SAMPLING_DISTANCE);
144         volume_sampling_method_enum.insert("equiangular", VOLUME_SAMPLING_EQUIANGULAR);
145         volume_sampling_method_enum.insert("multiple_importance", VOLUME_SAMPLING_MULTIPLE_IMPORTANCE);
146         SOCKET_ENUM(volume_sampling_method, "Volume Sampling Method", volume_sampling_method_enum, VOLUME_SAMPLING_DISTANCE);
147
148         static NodeEnum volume_interpolation_method_enum;
149         volume_interpolation_method_enum.insert("linear", VOLUME_INTERPOLATION_LINEAR);
150         volume_interpolation_method_enum.insert("cubic", VOLUME_INTERPOLATION_CUBIC);
151         SOCKET_ENUM(volume_interpolation_method, "Volume Interpolation Method", volume_interpolation_method_enum, VOLUME_INTERPOLATION_LINEAR);
152
153         return type;
154 }
155
156 Shader::Shader()
157 : Node(node_type)
158 {
159         pass_id = 0;
160
161         graph = NULL;
162         graph_bump = NULL;
163
164         has_surface = false;
165         has_surface_transparent = false;
166         has_surface_emission = false;
167         has_surface_bssrdf = false;
168         has_volume = false;
169         has_displacement = false;
170         has_bssrdf_bump = false;
171         has_surface_spatial_varying = false;
172         has_volume_spatial_varying = false;
173         has_object_dependency = false;
174         has_integrator_dependency = false;
175
176         id = -1;
177         used = false;
178
179         need_update = true;
180         need_update_attributes = true;
181 }
182
183 Shader::~Shader()
184 {
185         delete graph;
186         delete graph_bump;
187 }
188
189 void Shader::set_graph(ShaderGraph *graph_)
190 {
191         /* do this here already so that we can detect if mesh or object attributes
192          * are needed, since the node attribute callbacks check if their sockets
193          * are connected but proxy nodes should not count */
194         if(graph_)
195                 graph_->remove_proxy_nodes();
196
197         /* assign graph */
198         delete graph;
199         delete graph_bump;
200         graph = graph_;
201         graph_bump = NULL;
202 }
203
204 void Shader::tag_update(Scene *scene)
205 {
206         /* update tag */
207         need_update = true;
208         scene->shader_manager->need_update = true;
209
210         /* if the shader previously was emissive, update light distribution,
211          * if the new shader is emissive, a light manager update tag will be
212          * done in the shader manager device update. */
213         if(use_mis && has_surface_emission)
214                 scene->light_manager->need_update = true;
215
216         /* quick detection of which kind of shaders we have to avoid loading
217          * e.g. surface attributes when there is only a volume shader. this could
218          * be more fine grained but it's better than nothing */
219         OutputNode *output = graph->output();
220         bool prev_has_volume = has_volume;
221         has_surface = has_surface || output->input("Surface")->link;
222         has_volume = has_volume || output->input("Volume")->link;
223         has_displacement = has_displacement || output->input("Displacement")->link;
224
225         /* get requested attributes. this could be optimized by pruning unused
226          * nodes here already, but that's the job of the shader manager currently,
227          * and may not be so great for interactive rendering where you temporarily
228          * disconnect a node */
229
230         AttributeRequestSet prev_attributes = attributes;
231
232         attributes.clear();
233         foreach(ShaderNode *node, graph->nodes)
234                 node->attributes(this, &attributes);
235         
236         /* compare if the attributes changed, mesh manager will check
237          * need_update_attributes, update the relevant meshes and clear it. */
238         if(attributes.modified(prev_attributes)) {
239                 need_update_attributes = true;
240                 scene->mesh_manager->need_update = true;
241         }
242
243         if(has_volume != prev_has_volume) {
244                 scene->mesh_manager->need_flags_update = true;
245                 scene->object_manager->need_flags_update = true;
246         }
247 }
248
249 void Shader::tag_used(Scene *scene)
250 {
251         /* if an unused shader suddenly gets used somewhere, it needs to be
252          * recompiled because it was skipped for compilation before */
253         if(!used) {
254                 need_update = true;
255                 scene->shader_manager->need_update = true;
256         }
257 }
258
259 /* Shader Manager */
260
261 ShaderManager::ShaderManager()
262 {
263         need_update = true;
264         beckmann_table_offset = TABLE_OFFSET_INVALID;
265 }
266
267 ShaderManager::~ShaderManager()
268 {
269 }
270
271 ShaderManager *ShaderManager::create(Scene *scene, int shadingsystem)
272 {
273         ShaderManager *manager;
274
275         (void)shadingsystem;  /* Ignored when built without OSL. */
276
277 #ifdef WITH_OSL
278         if(shadingsystem == SHADINGSYSTEM_OSL)
279                 manager = new OSLShaderManager();
280         else
281 #endif
282                 manager = new SVMShaderManager();
283         
284         add_default(scene);
285
286         return manager;
287 }
288
289 uint ShaderManager::get_attribute_id(ustring name)
290 {
291         /* get a unique id for each name, for SVM attribute lookup */
292         AttributeIDMap::iterator it = unique_attribute_id.find(name);
293
294         if(it != unique_attribute_id.end())
295                 return it->second;
296         
297         uint id = (uint)ATTR_STD_NUM + unique_attribute_id.size();
298         unique_attribute_id[name] = id;
299         return id;
300 }
301
302 uint ShaderManager::get_attribute_id(AttributeStandard std)
303 {
304         return (uint)std;
305 }
306
307 int ShaderManager::get_shader_id(Shader *shader, Mesh *mesh, bool smooth)
308 {
309         /* get a shader id to pass to the kernel */
310         int id = shader->id*2;
311         
312         /* index depends bump since this setting is not in the shader */
313         if(mesh && mesh->displacement_method != Mesh::DISPLACE_TRUE)
314                 id += 1;
315         /* smooth flag */
316         if(smooth)
317                 id |= SHADER_SMOOTH_NORMAL;
318         
319         /* default flags */
320         id |= SHADER_CAST_SHADOW|SHADER_AREA_LIGHT;
321         
322         return id;
323 }
324
325 void ShaderManager::device_update_shaders_used(Scene *scene)
326 {
327         /* figure out which shaders are in use, so SVM/OSL can skip compiling them
328          * for speed and avoid loading image textures into memory */
329         uint id = 0;
330         foreach(Shader *shader, scene->shaders) {
331                 shader->used = false;
332                 shader->id = id++;
333         }
334
335         scene->default_surface->used = true;
336         scene->default_light->used = true;
337         scene->default_background->used = true;
338         scene->default_empty->used = true;
339
340         if(scene->background->shader)
341                 scene->background->shader->used = true;
342
343         foreach(Mesh *mesh, scene->meshes)
344                 foreach(Shader *shader, mesh->used_shaders)
345                         shader->used = true;
346
347         foreach(Light *light, scene->lights)
348                 if(light->shader)
349                         light->shader->used = true;
350 }
351
352 void ShaderManager::device_update_common(Device *device,
353                                          DeviceScene *dscene,
354                                          Scene *scene,
355                                          Progress& /*progress*/)
356 {
357         device->tex_free(dscene->shader_flag);
358         dscene->shader_flag.clear();
359
360         if(scene->shaders.size() == 0)
361                 return;
362
363         uint shader_flag_size = scene->shaders.size()*4;
364         uint *shader_flag = dscene->shader_flag.resize(shader_flag_size);
365         uint i = 0;
366         bool has_volumes = false;
367         bool has_transparent_shadow = false;
368
369         foreach(Shader *shader, scene->shaders) {
370                 uint flag = 0;
371
372                 if(shader->use_mis)
373                         flag |= SD_USE_MIS;
374                 if(shader->has_surface_transparent && shader->use_transparent_shadow)
375                         flag |= SD_HAS_TRANSPARENT_SHADOW;
376                 if(shader->has_volume) {
377                         flag |= SD_HAS_VOLUME;
378                         has_volumes = true;
379
380                         /* in this case we can assume transparent surface */
381                         if(!shader->has_surface)
382                                 flag |= SD_HAS_ONLY_VOLUME;
383
384                         /* todo: this could check more fine grained, to skip useless volumes
385                          * enclosed inside an opaque bsdf.
386                          */
387                         flag |= SD_HAS_TRANSPARENT_SHADOW;
388                 }
389                 if(shader->heterogeneous_volume && shader->has_volume_spatial_varying)
390                         flag |= SD_HETEROGENEOUS_VOLUME;
391                 if(shader->has_bssrdf_bump)
392                         flag |= SD_HAS_BSSRDF_BUMP;
393                 if(shader->volume_sampling_method == VOLUME_SAMPLING_EQUIANGULAR)
394                         flag |= SD_VOLUME_EQUIANGULAR;
395                 if(shader->volume_sampling_method == VOLUME_SAMPLING_MULTIPLE_IMPORTANCE)
396                         flag |= SD_VOLUME_MIS;
397                 if(shader->volume_interpolation_method == VOLUME_INTERPOLATION_CUBIC)
398                         flag |= SD_VOLUME_CUBIC;
399                 if(shader->graph_bump)
400                         flag |= SD_HAS_BUMP;
401
402                 /* regular shader */
403                 shader_flag[i++] = flag;
404                 shader_flag[i++] = shader->pass_id;
405
406                 /* shader with bump mapping */
407                 if(shader->graph_bump)
408                         flag |= SD_HAS_BSSRDF_BUMP;
409
410                 shader_flag[i++] = flag;
411                 shader_flag[i++] = shader->pass_id;
412
413                 has_transparent_shadow |= (flag & SD_HAS_TRANSPARENT_SHADOW) != 0;
414         }
415
416         device->tex_alloc("__shader_flag", dscene->shader_flag);
417
418         /* lookup tables */
419         KernelTables *ktables = &dscene->data.tables;
420
421         /* beckmann lookup table */
422         if(beckmann_table_offset == TABLE_OFFSET_INVALID) {
423                 if(beckmann_table.size() == 0) {
424                         thread_scoped_lock lock(lookup_table_mutex);
425                         if(beckmann_table.size() == 0) {
426                                 beckmann_table_build(beckmann_table);
427                         }
428                 }
429                 beckmann_table_offset = scene->lookup_tables->add_table(dscene, beckmann_table);
430         }
431         ktables->beckmann_offset = (int)beckmann_table_offset;
432
433         /* integrator */
434         KernelIntegrator *kintegrator = &dscene->data.integrator;
435         kintegrator->use_volumes = has_volumes;
436         /* TODO(sergey): De-duplicate with flags set in integrator.cpp. */
437         if(scene->integrator->transparent_shadows) {
438                 kintegrator->transparent_shadows = has_transparent_shadow;
439         }
440 }
441
442 void ShaderManager::device_free_common(Device *device, DeviceScene *dscene, Scene *scene)
443 {
444         scene->lookup_tables->remove_table(&beckmann_table_offset);
445
446         device->tex_free(dscene->shader_flag);
447         dscene->shader_flag.clear();
448 }
449
450 void ShaderManager::add_default(Scene *scene)
451 {
452         /* default surface */
453         {
454                 ShaderGraph *graph = new ShaderGraph();
455
456                 DiffuseBsdfNode *diffuse = new DiffuseBsdfNode();
457                 diffuse->color = make_float3(0.8f, 0.8f, 0.8f);
458                 graph->add(diffuse);
459
460                 graph->connect(diffuse->output("BSDF"), graph->output()->input("Surface"));
461
462                 Shader *shader = new Shader();
463                 shader->name = "default_surface";
464                 shader->graph = graph;
465                 scene->shaders.push_back(shader);
466                 scene->default_surface = shader;
467         }
468
469         /* default light */
470         {
471                 ShaderGraph *graph = new ShaderGraph();
472
473                 EmissionNode *emission = new EmissionNode();
474                 emission->color = make_float3(0.8f, 0.8f, 0.8f);
475                 emission->strength = 0.0f;
476                 graph->add(emission);
477
478                 graph->connect(emission->output("Emission"), graph->output()->input("Surface"));
479
480                 Shader *shader = new Shader();
481                 shader->name = "default_light";
482                 shader->graph = graph;
483                 scene->shaders.push_back(shader);
484                 scene->default_light = shader;
485         }
486
487         /* default background */
488         {
489                 ShaderGraph *graph = new ShaderGraph();
490
491                 Shader *shader = new Shader();
492                 shader->name = "default_background";
493                 shader->graph = graph;
494                 scene->shaders.push_back(shader);
495                 scene->default_background = shader;
496         }
497
498         /* default empty */
499         {
500                 ShaderGraph *graph = new ShaderGraph();
501
502                 Shader *shader = new Shader();
503                 shader->name = "default_empty";
504                 shader->graph = graph;
505                 scene->shaders.push_back(shader);
506                 scene->default_empty = shader;
507         }
508 }
509
510 void ShaderManager::get_requested_graph_features(ShaderGraph *graph,
511                                                  DeviceRequestedFeatures *requested_features)
512 {
513         foreach(ShaderNode *node, graph->nodes) {
514                 requested_features->max_nodes_group = max(requested_features->max_nodes_group,
515                                                           node->get_group());
516                 requested_features->nodes_features |= node->get_feature();
517                 if(node->special_type == SHADER_SPECIAL_TYPE_CLOSURE) {
518                         BsdfNode *bsdf_node = static_cast<BsdfNode*>(node);
519                         if(CLOSURE_IS_VOLUME(bsdf_node->closure)) {
520                                 requested_features->nodes_features |= NODE_FEATURE_VOLUME;
521                         }
522                 }
523                 if(node->has_surface_bssrdf()) {
524                         requested_features->use_subsurface = true;
525                 }
526         }
527 }
528
529 void ShaderManager::get_requested_features(Scene *scene,
530                                            DeviceRequestedFeatures *requested_features)
531 {
532         requested_features->max_nodes_group = NODE_GROUP_LEVEL_0;
533         requested_features->nodes_features = 0;
534         for(int i = 0; i < scene->shaders.size(); i++) {
535                 Shader *shader = scene->shaders[i];
536                 /* Gather requested features from all the nodes from the graph nodes. */
537                 get_requested_graph_features(shader->graph, requested_features);
538                 /* Gather requested features from the graph itself. */
539                 if(shader->graph_bump) {
540                         get_requested_graph_features(shader->graph_bump,
541                                                      requested_features);
542                 }
543                 ShaderNode *output_node = shader->graph->output();
544                 if(output_node->input("Displacement")->link != NULL) {
545                         requested_features->nodes_features |= NODE_FEATURE_BUMP;
546                 }
547                 /* On top of volume nodes, also check if we need volume sampling because
548                  * e.g. an Emission node would slip through the NODE_FEATURE_VOLUME check */
549                 if(shader->has_volume)
550                         requested_features->use_volume |= true;
551         }
552 }
553
554 void ShaderManager::free_memory()
555 {
556         beckmann_table.free_memory();
557 }
558
559 CCL_NAMESPACE_END
560