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