Fix Cycles viewport render not updating when tweaking displacement shader.
[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_integrator_dependency = false;
195         has_volume_connected = false;
196
197         displacement_method = DISPLACE_BUMP;
198
199         id = -1;
200         used = false;
201
202         need_update = true;
203         need_update_mesh = true;
204 }
205
206 Shader::~Shader()
207 {
208         delete graph;
209 }
210
211 bool Shader::is_constant_emission(float3 *emission)
212 {
213         ShaderInput *surf = graph->output()->input("Surface");
214
215         if(!surf->link || surf->link->parent->type != EmissionNode::node_type) {
216                 return false;
217         }
218
219         EmissionNode *node = (EmissionNode*) surf->link->parent;
220
221         assert(node->input("Color"));
222         assert(node->input("Strength"));
223
224         if(node->input("Color")->link || node->input("Strength")->link) {
225                 return false;
226         }
227
228         *emission = node->color*node->strength;
229
230         return true;
231 }
232
233 void Shader::set_graph(ShaderGraph *graph_)
234 {
235         /* do this here already so that we can detect if mesh or object attributes
236          * are needed, since the node attribute callbacks check if their sockets
237          * are connected but proxy nodes should not count */
238         if(graph_) {
239                 graph_->remove_proxy_nodes();
240
241                 if(displacement_method != DISPLACE_BUMP) {
242                         graph_->compute_displacement_hash();
243                 }
244         }
245
246         /* update geometry if displacement changed */
247         if(displacement_method != DISPLACE_BUMP) {
248                 const char *old_hash = (graph)? graph->displacement_hash.c_str() : "";
249                 const char *new_hash = (graph_)? graph_->displacement_hash.c_str() : "";
250
251                 if(strcmp(old_hash, new_hash) != 0) {
252                         need_update_mesh = true;
253                 }
254         }
255
256         /* assign graph */
257         delete graph;
258         graph = graph_;
259
260         /* Store info here before graph optimization to make sure that
261          * nodes that get optimized away still count. */
262         has_volume_connected = (graph->output()->input("Volume")->link != NULL);
263 }
264
265 void Shader::tag_update(Scene *scene)
266 {
267         /* update tag */
268         need_update = true;
269         scene->shader_manager->need_update = true;
270
271         /* if the shader previously was emissive, update light distribution,
272          * if the new shader is emissive, a light manager update tag will be
273          * done in the shader manager device update. */
274         if(use_mis && has_surface_emission)
275                 scene->light_manager->need_update = true;
276
277         /* Special handle of background MIS light for now: for some reason it
278          * has use_mis set to false. We are quite close to release now, so
279          * better to be safe.
280          */
281         if(this == scene->default_background &&
282            scene->light_manager->has_background_light(scene))
283         {
284                 scene->light_manager->need_update = true;
285         }
286
287         /* quick detection of which kind of shaders we have to avoid loading
288          * e.g. surface attributes when there is only a volume shader. this could
289          * be more fine grained but it's better than nothing */
290         OutputNode *output = graph->output();
291         bool prev_has_volume = has_volume;
292         has_surface = has_surface || output->input("Surface")->link;
293         has_volume = has_volume || output->input("Volume")->link;
294         has_displacement = has_displacement || output->input("Displacement")->link;
295
296         /* get requested attributes. this could be optimized by pruning unused
297          * nodes here already, but that's the job of the shader manager currently,
298          * and may not be so great for interactive rendering where you temporarily
299          * disconnect a node */
300
301         AttributeRequestSet prev_attributes = attributes;
302
303         attributes.clear();
304         foreach(ShaderNode *node, graph->nodes)
305                 node->attributes(this, &attributes);
306
307         if(has_displacement && displacement_method == DISPLACE_BOTH) {
308                 attributes.add(ATTR_STD_POSITION_UNDISPLACED);
309         }
310         
311         /* compare if the attributes changed, mesh manager will check
312          * need_update_mesh, update the relevant meshes and clear it. */
313         if(attributes.modified(prev_attributes)) {
314                 need_update_mesh = true;
315                 scene->mesh_manager->need_update = true;
316         }
317
318         if(has_volume != prev_has_volume) {
319                 scene->mesh_manager->need_flags_update = true;
320                 scene->object_manager->need_flags_update = true;
321         }
322 }
323
324 void Shader::tag_used(Scene *scene)
325 {
326         /* if an unused shader suddenly gets used somewhere, it needs to be
327          * recompiled because it was skipped for compilation before */
328         if(!used) {
329                 need_update = true;
330                 scene->shader_manager->need_update = true;
331         }
332 }
333
334 /* Shader Manager */
335
336 ShaderManager::ShaderManager()
337 {
338         need_update = true;
339         beckmann_table_offset = TABLE_OFFSET_INVALID;
340 }
341
342 ShaderManager::~ShaderManager()
343 {
344 }
345
346 ShaderManager *ShaderManager::create(Scene *scene, int shadingsystem)
347 {
348         ShaderManager *manager;
349
350         (void)shadingsystem;  /* Ignored when built without OSL. */
351
352 #ifdef WITH_OSL
353         if(shadingsystem == SHADINGSYSTEM_OSL) {
354                 manager = new OSLShaderManager();
355         }
356         else
357 #endif
358         {
359                 manager = new SVMShaderManager();
360         }
361         
362         add_default(scene);
363
364         return manager;
365 }
366
367 uint ShaderManager::get_attribute_id(ustring name)
368 {
369         thread_scoped_spin_lock lock(attribute_lock_);
370
371         /* get a unique id for each name, for SVM attribute lookup */
372         AttributeIDMap::iterator it = unique_attribute_id.find(name);
373
374         if(it != unique_attribute_id.end())
375                 return it->second;
376         
377         uint id = (uint)ATTR_STD_NUM + unique_attribute_id.size();
378         unique_attribute_id[name] = id;
379         return id;
380 }
381
382 uint ShaderManager::get_attribute_id(AttributeStandard std)
383 {
384         return (uint)std;
385 }
386
387 int ShaderManager::get_shader_id(Shader *shader, bool smooth)
388 {
389         /* get a shader id to pass to the kernel */
390         int id = shader->id;
391
392         /* smooth flag */
393         if(smooth)
394                 id |= SHADER_SMOOTH_NORMAL;
395         
396         /* default flags */
397         id |= SHADER_CAST_SHADOW|SHADER_AREA_LIGHT;
398         
399         return id;
400 }
401
402 void ShaderManager::device_update_shaders_used(Scene *scene)
403 {
404         /* figure out which shaders are in use, so SVM/OSL can skip compiling them
405          * for speed and avoid loading image textures into memory */
406         uint id = 0;
407         foreach(Shader *shader, scene->shaders) {
408                 shader->used = false;
409                 shader->id = id++;
410         }
411
412         scene->default_surface->used = true;
413         scene->default_light->used = true;
414         scene->default_background->used = true;
415         scene->default_empty->used = true;
416
417         if(scene->background->shader)
418                 scene->background->shader->used = true;
419
420         foreach(Mesh *mesh, scene->meshes)
421                 foreach(Shader *shader, mesh->used_shaders)
422                         shader->used = true;
423
424         foreach(Light *light, scene->lights)
425                 if(light->shader)
426                         light->shader->used = true;
427 }
428
429 void ShaderManager::device_update_common(Device *device,
430                                          DeviceScene *dscene,
431                                          Scene *scene,
432                                          Progress& /*progress*/)
433 {
434         dscene->shader_flag.free();
435
436         if(scene->shaders.size() == 0)
437                 return;
438
439         uint shader_flag_size = scene->shaders.size()*SHADER_SIZE;
440         uint *shader_flag = dscene->shader_flag.alloc(shader_flag_size);
441         uint i = 0;
442         bool has_volumes = false;
443         bool has_transparent_shadow = false;
444
445         foreach(Shader *shader, scene->shaders) {
446                 uint flag = 0;
447
448                 if(shader->use_mis)
449                         flag |= SD_USE_MIS;
450                 if(shader->has_surface_transparent && shader->use_transparent_shadow)
451                         flag |= SD_HAS_TRANSPARENT_SHADOW;
452                 if(shader->has_volume) {
453                         flag |= SD_HAS_VOLUME;
454                         has_volumes = true;
455
456                         /* todo: this could check more fine grained, to skip useless volumes
457                          * enclosed inside an opaque bsdf.
458                          */
459                         flag |= SD_HAS_TRANSPARENT_SHADOW;
460                 }
461                 /* in this case we can assume transparent surface */
462                 if(shader->has_volume_connected && !shader->has_surface)
463                         flag |= SD_HAS_ONLY_VOLUME;
464                 if(shader->heterogeneous_volume && shader->has_volume_spatial_varying)
465                         flag |= SD_HETEROGENEOUS_VOLUME;
466                 if(shader->has_bssrdf_bump)
467                         flag |= SD_HAS_BSSRDF_BUMP;
468                 if(device->info.has_volume_decoupled) {
469                         if(shader->volume_sampling_method == VOLUME_SAMPLING_EQUIANGULAR)
470                                 flag |= SD_VOLUME_EQUIANGULAR;
471                         if(shader->volume_sampling_method == VOLUME_SAMPLING_MULTIPLE_IMPORTANCE)
472                                 flag |= SD_VOLUME_MIS;
473                 }
474                 if(shader->volume_interpolation_method == VOLUME_INTERPOLATION_CUBIC)
475                         flag |= SD_VOLUME_CUBIC;
476                 if(shader->has_bump)
477                         flag |= SD_HAS_BUMP;
478                 if(shader->displacement_method != DISPLACE_BUMP)
479                         flag |= SD_HAS_DISPLACEMENT;
480
481                 /* constant emission check */
482                 float3 constant_emission = make_float3(0.0f, 0.0f, 0.0f);
483                 if(shader->is_constant_emission(&constant_emission))
484                         flag |= SD_HAS_CONSTANT_EMISSION;
485
486                 /* regular shader */
487                 shader_flag[i++] = flag;
488                 shader_flag[i++] = shader->pass_id;
489                 shader_flag[i++] = __float_as_int(constant_emission.x);
490                 shader_flag[i++] = __float_as_int(constant_emission.y);
491                 shader_flag[i++] = __float_as_int(constant_emission.z);
492
493                 has_transparent_shadow |= (flag & SD_HAS_TRANSPARENT_SHADOW) != 0;
494         }
495
496         dscene->shader_flag.copy_to_device();
497
498         /* lookup tables */
499         KernelTables *ktables = &dscene->data.tables;
500
501         /* beckmann lookup table */
502         if(beckmann_table_offset == TABLE_OFFSET_INVALID) {
503                 if(!beckmann_table_ready) {
504                         thread_scoped_lock lock(lookup_table_mutex);
505                         if(!beckmann_table_ready) {
506                                 beckmann_table_build(beckmann_table);
507                                 beckmann_table_ready = true;
508                         }
509                 }
510                 beckmann_table_offset = scene->lookup_tables->add_table(dscene, beckmann_table);
511         }
512         ktables->beckmann_offset = (int)beckmann_table_offset;
513
514         /* integrator */
515         KernelIntegrator *kintegrator = &dscene->data.integrator;
516         kintegrator->use_volumes = has_volumes;
517         /* TODO(sergey): De-duplicate with flags set in integrator.cpp. */
518         kintegrator->transparent_shadows = has_transparent_shadow;
519 }
520
521 void ShaderManager::device_free_common(Device *, DeviceScene *dscene, Scene *scene)
522 {
523         scene->lookup_tables->remove_table(&beckmann_table_offset);
524
525         dscene->shader_flag.free();
526 }
527
528 void ShaderManager::add_default(Scene *scene)
529 {
530         /* default surface */
531         {
532                 ShaderGraph *graph = new ShaderGraph();
533
534                 DiffuseBsdfNode *diffuse = new DiffuseBsdfNode();
535                 diffuse->color = make_float3(0.8f, 0.8f, 0.8f);
536                 graph->add(diffuse);
537
538                 graph->connect(diffuse->output("BSDF"), graph->output()->input("Surface"));
539
540                 Shader *shader = new Shader();
541                 shader->name = "default_surface";
542                 shader->graph = graph;
543                 scene->shaders.push_back(shader);
544                 scene->default_surface = shader;
545         }
546
547         /* default light */
548         {
549                 ShaderGraph *graph = new ShaderGraph();
550
551                 EmissionNode *emission = new EmissionNode();
552                 emission->color = make_float3(0.8f, 0.8f, 0.8f);
553                 emission->strength = 0.0f;
554                 graph->add(emission);
555
556                 graph->connect(emission->output("Emission"), graph->output()->input("Surface"));
557
558                 Shader *shader = new Shader();
559                 shader->name = "default_light";
560                 shader->graph = graph;
561                 scene->shaders.push_back(shader);
562                 scene->default_light = shader;
563         }
564
565         /* default background */
566         {
567                 ShaderGraph *graph = new ShaderGraph();
568
569                 Shader *shader = new Shader();
570                 shader->name = "default_background";
571                 shader->graph = graph;
572                 scene->shaders.push_back(shader);
573                 scene->default_background = shader;
574         }
575
576         /* default empty */
577         {
578                 ShaderGraph *graph = new ShaderGraph();
579
580                 Shader *shader = new Shader();
581                 shader->name = "default_empty";
582                 shader->graph = graph;
583                 scene->shaders.push_back(shader);
584                 scene->default_empty = shader;
585         }
586 }
587
588 void ShaderManager::get_requested_graph_features(ShaderGraph *graph,
589                                                  DeviceRequestedFeatures *requested_features)
590 {
591         foreach(ShaderNode *node, graph->nodes) {
592                 requested_features->max_nodes_group = max(requested_features->max_nodes_group,
593                                                           node->get_group());
594                 requested_features->nodes_features |= node->get_feature();
595                 if(node->special_type == SHADER_SPECIAL_TYPE_CLOSURE) {
596                         BsdfNode *bsdf_node = static_cast<BsdfNode*>(node);
597                         if(CLOSURE_IS_VOLUME(bsdf_node->closure)) {
598                                 requested_features->nodes_features |= NODE_FEATURE_VOLUME;
599                         }
600                         else if(CLOSURE_IS_PRINCIPLED(bsdf_node->closure)) {
601                                 requested_features->use_principled = true;
602                         }
603                 }
604                 if(node->has_surface_bssrdf()) {
605                         requested_features->use_subsurface = true;
606                 }
607                 if(node->has_surface_transparent()) {
608                         requested_features->use_transparent = true;
609                 }
610                 if(node->has_raytrace()) {
611                         requested_features->use_shader_raytrace = true;
612                 }
613         }
614 }
615
616 void ShaderManager::get_requested_features(Scene *scene,
617                                            DeviceRequestedFeatures *requested_features)
618 {
619         requested_features->max_nodes_group = NODE_GROUP_LEVEL_0;
620         requested_features->nodes_features = 0;
621         for(int i = 0; i < scene->shaders.size(); i++) {
622                 Shader *shader = scene->shaders[i];
623                 /* Gather requested features from all the nodes from the graph nodes. */
624                 get_requested_graph_features(shader->graph, requested_features);
625                 ShaderNode *output_node = shader->graph->output();
626                 if(output_node->input("Displacement")->link != NULL) {
627                         requested_features->nodes_features |= NODE_FEATURE_BUMP;
628                         if(shader->displacement_method == DISPLACE_BOTH && requested_features->experimental) {
629                                 requested_features->nodes_features |= NODE_FEATURE_BUMP_STATE;
630                         }
631                 }
632                 /* On top of volume nodes, also check if we need volume sampling because
633                  * e.g. an Emission node would slip through the NODE_FEATURE_VOLUME check */
634                 if(shader->has_volume)
635                         requested_features->use_volume |= true;
636         }
637 }
638
639 void ShaderManager::free_memory()
640 {
641         beckmann_table.free_memory();
642 }
643
644 CCL_NAMESPACE_END
645