e5faf307110470ce079bfb5e1bfdd4e07d885b98
[blender.git] / intern / cycles / render / svm.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 "device.h"
18 #include "graph.h"
19 #include "light.h"
20 #include "mesh.h"
21 #include "nodes.h"
22 #include "scene.h"
23 #include "shader.h"
24 #include "svm.h"
25
26 #include "util_debug.h"
27 #include "util_logging.h"
28 #include "util_foreach.h"
29 #include "util_progress.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 /* Shader Manager */
34
35 SVMShaderManager::SVMShaderManager()
36 {
37 }
38
39 SVMShaderManager::~SVMShaderManager()
40 {
41 }
42
43 void SVMShaderManager::reset(Scene * /*scene*/)
44 {
45 }
46
47 void SVMShaderManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
48 {
49         VLOG(1) << "Total " << scene->shaders.size() << " shaders.";
50
51         if(!need_update)
52                 return;
53
54         /* test if we need to update */
55         device_free(device, dscene, scene);
56
57         /* determine which shaders are in use */
58         device_update_shaders_used(scene);
59
60         /* svm_nodes */
61         vector<int4> svm_nodes;
62         size_t i;
63
64         for(i = 0; i < scene->shaders.size(); i++) {
65                 svm_nodes.push_back(make_int4(NODE_SHADER_JUMP, 0, 0, 0));
66                 svm_nodes.push_back(make_int4(NODE_SHADER_JUMP, 0, 0, 0));
67         }
68         
69         for(i = 0; i < scene->shaders.size(); i++) {
70                 Shader *shader = scene->shaders[i];
71
72                 if(progress.get_cancel()) return;
73
74                 assert(shader->graph);
75
76                 if(shader->use_mis && shader->has_surface_emission)
77                         scene->light_manager->need_update = true;
78
79                 SVMCompiler::Summary summary;
80                 SVMCompiler compiler(scene->shader_manager, scene->image_manager);
81                 compiler.background = ((int)i == scene->default_background);
82                 compiler.compile(scene, shader, svm_nodes, i, &summary);
83
84                 VLOG(1) << "Compilation summary:\n"
85                         << "Shader name: " << shader->name << "\n"
86                         << summary.full_report();
87         }
88
89         dscene->svm_nodes.copy((uint4*)&svm_nodes[0], svm_nodes.size());
90         device->tex_alloc("__svm_nodes", dscene->svm_nodes);
91
92         for(i = 0; i < scene->shaders.size(); i++) {
93                 Shader *shader = scene->shaders[i];
94                 shader->need_update = false;
95         }
96
97         device_update_common(device, dscene, scene, progress);
98
99         need_update = false;
100 }
101
102 void SVMShaderManager::device_free(Device *device, DeviceScene *dscene, Scene *scene)
103 {
104         device_free_common(device, dscene, scene);
105
106         device->tex_free(dscene->svm_nodes);
107         dscene->svm_nodes.clear();
108 }
109
110 /* Graph Compiler */
111
112 SVMCompiler::SVMCompiler(ShaderManager *shader_manager_, ImageManager *image_manager_)
113 {
114         shader_manager = shader_manager_;
115         image_manager = image_manager_;
116         max_stack_use = 0;
117         current_type = SHADER_TYPE_SURFACE;
118         current_shader = NULL;
119         current_graph = NULL;
120         background = false;
121         mix_weight_offset = SVM_STACK_INVALID;
122         compile_failed = false;
123 }
124
125 int SVMCompiler::stack_size(ShaderSocketType type)
126 {
127         int size = 0;
128         
129         switch(type) {
130                 case SHADER_SOCKET_FLOAT:
131                 case SHADER_SOCKET_INT:
132                         size = 1;
133                         break;
134                 case SHADER_SOCKET_COLOR:
135                 case SHADER_SOCKET_VECTOR:
136                 case SHADER_SOCKET_NORMAL:
137                 case SHADER_SOCKET_POINT:
138                         size = 3;
139                         break;
140                 case SHADER_SOCKET_CLOSURE:
141                         size = 0;
142                         break;
143                 default:
144                         assert(0);
145                         break;
146         }
147         
148         return size;
149 }
150
151 int SVMCompiler::stack_find_offset(ShaderSocketType type)
152 {
153         int size = stack_size(type);
154         int offset = -1;
155         
156         /* find free space in stack & mark as used */
157         for(int i = 0, num_unused = 0; i < SVM_STACK_SIZE; i++) {
158                 if(active_stack.users[i]) num_unused = 0;
159                 else num_unused++;
160
161                 if(num_unused == size) {
162                         offset = i+1 - size;
163                         max_stack_use = max(i+1, max_stack_use);
164
165                         while(i >= offset)
166                                 active_stack.users[i--] = 1;
167
168                         return offset;
169                 }
170         }
171
172         if(!compile_failed) {
173                 compile_failed = true;
174                 fprintf(stderr, "Cycles: out of SVM stack space, shader \"%s\" too big.\n", current_shader->name.c_str());
175         }
176
177         return 0;
178 }
179
180 void SVMCompiler::stack_clear_offset(ShaderSocketType type, int offset)
181 {
182         int size = stack_size(type);
183
184         for(int i = 0; i < size; i++)
185                 active_stack.users[offset + i]--;
186 }
187
188 void SVMCompiler::stack_backup(StackBackup& backup, ShaderNodeSet& done)
189 {
190         backup.done = done;
191         backup.stack = active_stack;
192
193         foreach(ShaderNode *node, current_graph->nodes) {
194                 foreach(ShaderInput *input, node->inputs)
195                         backup.offsets.push_back(input->stack_offset);
196                 foreach(ShaderOutput *output, node->outputs)
197                         backup.offsets.push_back(output->stack_offset);
198         }
199 }
200
201 void SVMCompiler::stack_restore(StackBackup& backup, ShaderNodeSet& done)
202 {
203         int i = 0;
204
205         done = backup.done;
206         active_stack = backup.stack;
207
208         foreach(ShaderNode *node, current_graph->nodes) {
209                 foreach(ShaderInput *input, node->inputs)
210                         input->stack_offset = backup.offsets[i++];
211                 foreach(ShaderOutput *output, node->outputs)
212                         output->stack_offset = backup.offsets[i++];
213         }
214 }
215
216 void SVMCompiler::stack_assign(ShaderInput *input)
217 {
218         /* stack offset assign? */
219         if(input->stack_offset == SVM_STACK_INVALID) {
220                 if(input->link) {
221                         /* linked to output -> use output offset */
222                         input->stack_offset = input->link->stack_offset;
223                 }
224                 else {
225                         /* not linked to output -> add nodes to load default value */
226                         input->stack_offset = stack_find_offset(input->type);
227
228                         if(input->type == SHADER_SOCKET_FLOAT) {
229                                 add_node(NODE_VALUE_F, __float_as_int(input->value.x), input->stack_offset);
230                         }
231                         else if(input->type == SHADER_SOCKET_INT) {
232                                 add_node(NODE_VALUE_F, (int)input->value.x, input->stack_offset);
233                         }
234                         else if(input->type == SHADER_SOCKET_VECTOR ||
235                                 input->type == SHADER_SOCKET_NORMAL ||
236                                 input->type == SHADER_SOCKET_POINT ||
237                                 input->type == SHADER_SOCKET_COLOR)
238                         {
239
240                                 add_node(NODE_VALUE_V, input->stack_offset);
241                                 add_node(NODE_VALUE_V, input->value);
242                         }
243                         else /* should not get called for closure */
244                                 assert(0);
245                 }
246         }
247 }
248
249 void SVMCompiler::stack_assign(ShaderOutput *output)
250 {
251         /* if no stack offset assigned yet, find one */
252         if(output->stack_offset == SVM_STACK_INVALID)
253                 output->stack_offset = stack_find_offset(output->type);
254 }
255
256 void SVMCompiler::stack_link(ShaderInput *input, ShaderOutput *output)
257 {
258         if(output->stack_offset == SVM_STACK_INVALID) {
259                 assert(input->link);
260                 assert(stack_size(output->type) == stack_size(input->link->type));
261
262                 output->stack_offset = input->link->stack_offset;
263
264                 int size = stack_size(output->type);
265
266                 for(int i = 0; i < size; i++)
267                         active_stack.users[output->stack_offset + i]++;
268         }
269 }
270
271 void SVMCompiler::stack_clear_users(ShaderNode *node, ShaderNodeSet& done)
272 {
273         /* optimization we should add:
274          * find and lower user counts for outputs for which all inputs are done.
275          * this is done before the node is compiled, under the assumption that the
276          * node will first load all inputs from the stack and then writes its
277          * outputs. this used to work, but was disabled because it gave trouble
278          * with inputs getting stack positions assigned */
279
280         foreach(ShaderInput *input, node->inputs) {
281                 ShaderOutput *output = input->link;
282
283                 if(output && output->stack_offset != SVM_STACK_INVALID) {
284                         bool all_done = true;
285
286                         /* optimization we should add: verify if in->parent is actually used */
287                         foreach(ShaderInput *in, output->links)
288                                 if(in->parent != node && done.find(in->parent) == done.end())
289                                         all_done = false;
290
291                         if(all_done) {
292                                 stack_clear_offset(output->type, output->stack_offset);
293                                 output->stack_offset = SVM_STACK_INVALID;
294
295                                 foreach(ShaderInput *in, output->links)
296                                         in->stack_offset = SVM_STACK_INVALID;
297                         }
298                 }
299         }
300 }
301
302 void SVMCompiler::stack_clear_temporary(ShaderNode *node)
303 {
304         foreach(ShaderInput *input, node->inputs) {
305                 if(!input->link && input->stack_offset != SVM_STACK_INVALID) {
306                         stack_clear_offset(input->type, input->stack_offset);
307                         input->stack_offset = SVM_STACK_INVALID;
308                 }
309         }
310 }
311
312 uint SVMCompiler::encode_uchar4(uint x, uint y, uint z, uint w)
313 {
314         assert(x <= 255);
315         assert(y <= 255);
316         assert(z <= 255);
317         assert(w <= 255);
318
319         return (x) | (y << 8) | (z << 16) | (w << 24);
320 }
321
322 void SVMCompiler::add_node(int a, int b, int c, int d)
323 {
324         svm_nodes.push_back(make_int4(a, b, c, d));
325 }
326
327 void SVMCompiler::add_node(NodeType type, int a, int b, int c)
328 {
329         svm_nodes.push_back(make_int4(type, a, b, c));
330 }
331
332 void SVMCompiler::add_node(NodeType type, const float3& f)
333 {
334         svm_nodes.push_back(make_int4(type,
335                 __float_as_int(f.x),
336                 __float_as_int(f.y),
337                 __float_as_int(f.z)));
338 }
339
340 void SVMCompiler::add_node(const float4& f)
341 {
342         svm_nodes.push_back(make_int4(
343                 __float_as_int(f.x),
344                 __float_as_int(f.y),
345                 __float_as_int(f.z),
346                 __float_as_int(f.w)));
347 }
348
349 void SVMCompiler::add_array(float4 *f, int num)
350 {
351         for(int i = 0; i < num; i++)
352                 add_node(f[i]);
353 }
354
355 uint SVMCompiler::attribute(ustring name)
356 {
357         return shader_manager->get_attribute_id(name);
358 }
359
360 uint SVMCompiler::attribute(AttributeStandard std)
361 {
362         return shader_manager->get_attribute_id(std);
363 }
364
365 bool SVMCompiler::node_skip_input(ShaderNode * /*node*/, ShaderInput *input)
366 {
367         /* nasty exception .. */
368         if(current_type == SHADER_TYPE_DISPLACEMENT && input->link && input->link->parent->name == ustring("bump"))
369                 return true;
370         
371         return false;
372 }
373
374 void SVMCompiler::find_dependencies(ShaderNodeSet& dependencies,
375                                     const ShaderNodeSet& done,
376                                     ShaderInput *input,
377                                     ShaderNode *skip_node)
378 {
379         ShaderNode *node = (input->link)? input->link->parent: NULL;
380
381         if(node != NULL &&
382            done.find(node) == done.end() &&
383            node != skip_node &&
384            dependencies.find(node) == dependencies.end())
385         {
386                 foreach(ShaderInput *in, node->inputs)
387                         if(!node_skip_input(node, in))
388                                 find_dependencies(dependencies, done, in, skip_node);
389
390                 dependencies.insert(node);
391         }
392 }
393
394 void SVMCompiler::generate_node(ShaderNode *node, ShaderNodeSet& done)
395 {
396         node->compile(*this);
397         stack_clear_users(node, done);
398         stack_clear_temporary(node);
399
400         if(current_type == SHADER_TYPE_VOLUME) {
401                 if(node->has_spatial_varying())
402                         current_shader->has_heterogeneous_volume = true;
403         }
404
405         if(node->has_object_dependency()) {
406                 current_shader->has_object_dependency = true;
407         }
408
409         if(node->has_integrator_dependency()) {
410                 current_shader->has_integrator_dependency = true;
411         }
412 }
413
414 void SVMCompiler::generate_svm_nodes(const ShaderNodeSet& nodes, ShaderNodeSet& done)
415 {
416         bool nodes_done;
417
418         do {
419                 nodes_done = true;
420
421                 foreach(ShaderNode *node, nodes) {
422                         if(done.find(node) == done.end()) {
423                                 bool inputs_done = true;
424
425                                 foreach(ShaderInput *input, node->inputs)
426                                         if(!node_skip_input(node, input))
427                                                 if(input->link && done.find(input->link->parent) == done.end())
428                                                         inputs_done = false;
429
430                                 if(inputs_done) {
431                                         generate_node(node, done);
432                                         done.insert(node);
433                                 }
434                                 else
435                                         nodes_done = false;
436                         }
437                 }
438         } while(!nodes_done);
439 }
440
441 void SVMCompiler::generate_closure_node(ShaderNode *node, ShaderNodeSet& done)
442 {
443         /* execute dependencies for closure */
444         foreach(ShaderInput *in, node->inputs) {
445                 if(!node_skip_input(node, in) && in->link) {
446                         ShaderNodeSet dependencies;
447                         find_dependencies(dependencies, done, in);
448                         generate_svm_nodes(dependencies, done);
449                 }
450         }
451
452         /* closure mix weight */
453         const char *weight_name = (current_type == SHADER_TYPE_VOLUME)? "VolumeMixWeight": "SurfaceMixWeight";
454         ShaderInput *weight_in = node->input(weight_name);
455
456         if(weight_in && (weight_in->link || weight_in->value.x != 1.0f)) {
457                 stack_assign(weight_in);
458                 mix_weight_offset = weight_in->stack_offset;
459         }
460         else
461                 mix_weight_offset = SVM_STACK_INVALID;
462
463         /* compile closure itself */
464         generate_node(node, done);
465
466         mix_weight_offset = SVM_STACK_INVALID;
467
468         if(current_type == SHADER_TYPE_SURFACE) {
469                 if(node->has_surface_emission())
470                         current_shader->has_surface_emission = true;
471                 if(node->has_surface_transparent())
472                         current_shader->has_surface_transparent = true;
473                 if(node->has_surface_bssrdf()) {
474                         current_shader->has_surface_bssrdf = true;
475                         if(node->has_bssrdf_bump())
476                                 current_shader->has_bssrdf_bump = true;
477                 }
478         }
479 }
480
481 void SVMCompiler::generated_shared_closure_nodes(ShaderNode *root_node,
482                                                  ShaderNode *node,
483                                                  ShaderNodeSet& done,
484                                                  ShaderNodeSet& closure_done,
485                                                  const ShaderNodeSet& shared)
486 {
487         if(shared.find(node) != shared.end()) {
488                 generate_multi_closure(root_node, node, done, closure_done);
489         }
490         else {
491                 foreach(ShaderInput *in, node->inputs) {
492                         if(in->type == SHADER_SOCKET_CLOSURE && in->link)
493                                 generated_shared_closure_nodes(root_node, in->link->parent,
494                                                                done, closure_done, shared);
495                 }
496         }
497 }
498
499 void SVMCompiler::generate_multi_closure(ShaderNode *root_node,
500                                          ShaderNode *node,
501                                          ShaderNodeSet& done,
502                                          ShaderNodeSet& closure_done)
503 {
504         /* only generate once */
505         if(closure_done.find(node) != closure_done.end())
506                 return;
507
508         closure_done.insert(node);
509
510         if(node->name == ustring("mix_closure") || node->name == ustring("add_closure")) {
511                 /* weighting is already taken care of in ShaderGraph::transform_multi_closure */
512                 ShaderInput *cl1in = node->input("Closure1");
513                 ShaderInput *cl2in = node->input("Closure2");
514                 ShaderInput *facin = node->input("Fac");
515
516                 /* skip empty mix/add closure nodes */
517                 if(!cl1in->link && !cl2in->link)
518                         return;
519
520                 if(facin && facin->link) {
521                         /* mix closure: generate instructions to compute mix weight */
522                         ShaderNodeSet dependencies;
523                         find_dependencies(dependencies, done, facin);
524                         generate_svm_nodes(dependencies, done);
525
526                         stack_assign(facin);
527
528                         /* execute shared dependencies. this is needed to allow skipping
529                          * of zero weight closures and their dependencies later, so we
530                          * ensure that they only skip dependencies that are unique to them */
531                         ShaderNodeSet cl1deps, cl2deps, shareddeps;
532
533                         find_dependencies(cl1deps, done, cl1in);
534                         find_dependencies(cl2deps, done, cl2in);
535
536                         ShaderNodeIDComparator node_id_comp;
537                         set_intersection(cl1deps.begin(), cl1deps.end(),
538                                          cl2deps.begin(), cl2deps.end(),
539                                          std::inserter(shareddeps, shareddeps.begin()),
540                                          node_id_comp);
541
542                         /* it's possible some nodes are not shared between this mix node
543                          * inputs, but still needed to be always executed, this mainly
544                          * happens when a node of current subbranch is used by a parent
545                          * node or so */
546                         if(root_node != node) {
547                                 foreach(ShaderInput *in, root_node->inputs) {
548                                         ShaderNodeSet rootdeps;
549                                         find_dependencies(rootdeps, done, in, node);
550                                         set_intersection(rootdeps.begin(), rootdeps.end(),
551                                                          cl1deps.begin(), cl1deps.end(),
552                                                          std::inserter(shareddeps, shareddeps.begin()),
553                                                          node_id_comp);
554                                         set_intersection(rootdeps.begin(), rootdeps.end(),
555                                                          cl2deps.begin(), cl2deps.end(),
556                                                          std::inserter(shareddeps, shareddeps.begin()),
557                                                          node_id_comp);
558                                 }
559                         }
560
561                         if(!shareddeps.empty()) {
562                                 if(cl1in->link) {
563                                         generated_shared_closure_nodes(root_node, cl1in->link->parent,
564                                                                        done, closure_done, shareddeps);
565                                 }
566                                 if(cl2in->link) {
567                                         generated_shared_closure_nodes(root_node, cl2in->link->parent,
568                                                                        done, closure_done, shareddeps);
569                                 }
570
571                                 generate_svm_nodes(shareddeps, done);
572                         }
573
574                         /* generate instructions for input closure 1 */
575                         if(cl1in->link) {
576                                 /* add instruction to skip closure and its dependencies if mix weight is zero */
577                                 svm_nodes.push_back(make_int4(NODE_JUMP_IF_ONE, 0, facin->stack_offset, 0));
578                                 int node_jump_skip_index = svm_nodes.size() - 1;
579
580                                 generate_multi_closure(root_node, cl1in->link->parent, done, closure_done);
581
582                                 /* fill in jump instruction location to be after closure */
583                                 svm_nodes[node_jump_skip_index].y = svm_nodes.size() - node_jump_skip_index - 1;
584                         }
585
586                         /* generate instructions for input closure 2 */
587                         if(cl2in->link) {
588                                 /* add instruction to skip closure and its dependencies if mix weight is zero */
589                                 svm_nodes.push_back(make_int4(NODE_JUMP_IF_ZERO, 0, facin->stack_offset, 0));
590                                 int node_jump_skip_index = svm_nodes.size() - 1;
591
592                                 generate_multi_closure(root_node, cl2in->link->parent, done, closure_done);
593
594                                 /* fill in jump instruction location to be after closure */
595                                 svm_nodes[node_jump_skip_index].y = svm_nodes.size() - node_jump_skip_index - 1;
596                         }
597
598                         /* unassign */
599                         facin->stack_offset = SVM_STACK_INVALID;
600                 }
601                 else {
602                         /* execute closures and their dependencies, no runtime checks
603                          * to skip closures here because was already optimized due to
604                          * fixed weight or add closure that always needs both */
605                         if(cl1in->link)
606                                 generate_multi_closure(root_node, cl1in->link->parent, done, closure_done);
607                         if(cl2in->link)
608                                 generate_multi_closure(root_node, cl2in->link->parent, done, closure_done);
609                 }
610         }
611         else {
612                 generate_closure_node(node, done);
613         }
614
615         done.insert(node);
616 }
617
618
619 void SVMCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
620 {
621         /* Converting a shader graph into svm_nodes that can be executed
622          * sequentially on the virtual machine is fairly simple. We can keep
623          * looping over nodes and each time all the inputs of a node are
624          * ready, we add svm_nodes for it that read the inputs from the
625          * stack and write outputs back to the stack.
626          *
627          * With the SVM, we always sample only a single closure. We can think
628          * of all closures nodes as a binary tree with mix closures as inner
629          * nodes and other closures as leafs. The SVM will traverse that tree,
630          * each time deciding to go left or right depending on the mix weights,
631          * until a closure is found.
632          *
633          * We only execute nodes that are needed for the mix weights and chosen
634          * closure.
635          */
636
637         current_type = type;
638         current_graph = graph;
639
640         /* get input in output node */
641         ShaderNode *node = graph->output();
642         ShaderInput *clin = NULL;
643         
644         switch(type) {
645                 case SHADER_TYPE_SURFACE:
646                         clin = node->input("Surface");
647                         break;
648                 case SHADER_TYPE_VOLUME:
649                         clin = node->input("Volume");
650                         break;
651                 case SHADER_TYPE_DISPLACEMENT:
652                         clin = node->input("Displacement");
653                         break;
654                 default:
655                         assert(0);
656                         break;
657         }
658
659         /* clear all compiler state */
660         memset(&active_stack, 0, sizeof(active_stack));
661         svm_nodes.clear();
662
663         foreach(ShaderNode *node_iter, graph->nodes) {
664                 foreach(ShaderInput *input, node_iter->inputs)
665                         input->stack_offset = SVM_STACK_INVALID;
666                 foreach(ShaderOutput *output, node_iter->outputs)
667                         output->stack_offset = SVM_STACK_INVALID;
668         }
669
670         if(shader->used) {
671                 if(clin->link) {
672                         bool generate = false;
673                         
674                         switch(type) {
675                                 case SHADER_TYPE_SURFACE: /* generate surface shader */         
676                                         generate = true;
677                                         shader->has_surface = true;
678                                         break;
679                                 case SHADER_TYPE_VOLUME: /* generate volume shader */
680                                         generate = true;
681                                         shader->has_volume = true;
682                                         break;
683                                 case SHADER_TYPE_DISPLACEMENT: /* generate displacement shader */
684                                         generate = true;
685                                         shader->has_displacement = true;
686                                         break;
687                                 default:
688                                         break;
689                         }
690
691                         if(generate) {
692                                 ShaderNodeSet done, closure_done;
693                                 generate_multi_closure(clin->link->parent, clin->link->parent,
694                                                        done, closure_done);
695                         }
696                 }
697
698                 /* compile output node */
699                 node->compile(*this);
700         }
701
702         /* if compile failed, generate empty shader */
703         if(compile_failed) {
704                 svm_nodes.clear();
705                 compile_failed = false;
706         }
707
708         add_node(NODE_END, 0, 0, 0);
709 }
710
711 void SVMCompiler::compile(Scene *scene,
712                           Shader *shader,
713                           vector<int4>& global_svm_nodes,
714                           int index,
715                           Summary *summary)
716 {
717         /* copy graph for shader with bump mapping */
718         ShaderNode *node = shader->graph->output();
719         int start_num_svm_nodes = global_svm_nodes.size();
720
721         const double time_total_start = time_dt();
722         double time_finalize = 0.0, time_finalize_bump = 0.0;
723         double time_generate_surface = 0.0,
724                time_generate_bump = 0.0,
725                time_generate_volume = 0.0,
726                time_generate_displacement = 0.0;
727
728         if(node->input("Surface")->link && node->input("Displacement")->link)
729                 if(!shader->graph_bump)
730                         shader->graph_bump = shader->graph->copy();
731
732         /* finalize */
733         double time_start = time_dt();
734         shader->graph->finalize(scene,
735                                 false,
736                                 false,
737                                 shader->has_integrator_dependency);
738         time_finalize = time_dt() - time_start;
739         if(shader->graph_bump) {
740                 time_start = time_dt();
741                 shader->graph_bump->finalize(scene,
742                                              true,
743                                              false,
744                                              shader->has_integrator_dependency);
745                 time_finalize_bump = time_dt() - time_start;
746         }
747
748         current_shader = shader;
749
750         shader->has_surface = false;
751         shader->has_surface_emission = false;
752         shader->has_surface_transparent = false;
753         shader->has_surface_bssrdf = false;
754         shader->has_bssrdf_bump = false;
755         shader->has_volume = false;
756         shader->has_displacement = false;
757         shader->has_heterogeneous_volume = false;
758         shader->has_object_dependency = false;
759         shader->has_integrator_dependency = false;
760
761         /* generate surface shader */
762         /* TODO(sergey): Add some utility to evaluate time of a scope. */
763         time_start = time_dt();
764         compile_type(shader, shader->graph, SHADER_TYPE_SURFACE);
765         global_svm_nodes[index*2 + 0].y = global_svm_nodes.size();
766         global_svm_nodes[index*2 + 1].y = global_svm_nodes.size();
767         global_svm_nodes.insert(global_svm_nodes.end(), svm_nodes.begin(), svm_nodes.end());
768         time_generate_surface = time_dt() - time_start;
769
770         if(shader->graph_bump) {
771                 time_start = time_dt();
772                 compile_type(shader, shader->graph_bump, SHADER_TYPE_SURFACE);
773                 global_svm_nodes[index*2 + 1].y = global_svm_nodes.size();
774                 global_svm_nodes.insert(global_svm_nodes.end(), svm_nodes.begin(), svm_nodes.end());
775                 time_generate_bump = time_dt() - time_start;
776         }
777
778         /* generate volume shader */
779         time_start = time_dt();
780         compile_type(shader, shader->graph, SHADER_TYPE_VOLUME);
781         global_svm_nodes[index*2 + 0].z = global_svm_nodes.size();
782         global_svm_nodes[index*2 + 1].z = global_svm_nodes.size();
783         global_svm_nodes.insert(global_svm_nodes.end(), svm_nodes.begin(), svm_nodes.end());
784         time_generate_volume = time_dt() - time_start;
785
786         /* generate displacement shader */
787         time_start = time_dt();
788         compile_type(shader, shader->graph, SHADER_TYPE_DISPLACEMENT);
789         global_svm_nodes[index*2 + 0].w = global_svm_nodes.size();
790         global_svm_nodes[index*2 + 1].w = global_svm_nodes.size();
791         global_svm_nodes.insert(global_svm_nodes.end(), svm_nodes.begin(), svm_nodes.end());
792         time_generate_displacement = time_dt() - time_start;
793
794         /* Fill in summary information. */
795         if(summary != NULL) {
796                 summary->time_total = time_dt() - time_total_start;
797                 summary->time_finalize = time_finalize;
798                 summary->time_finalize_bump = time_finalize_bump;
799                 summary->time_generate_surface = time_generate_surface;
800                 summary->time_generate_bump = time_generate_bump;
801                 summary->time_generate_volume = time_generate_volume;
802                 summary->time_generate_displacement = time_generate_displacement;
803                 summary->peak_stack_usage = max_stack_use;
804                 summary->num_svm_nodes = global_svm_nodes.size() - start_num_svm_nodes;
805         }
806 }
807
808 /* Compiler summary implementation. */
809
810 SVMCompiler::Summary::Summary()
811         : num_svm_nodes(0),
812           peak_stack_usage(0),
813           time_finalize(0.0),
814           time_finalize_bump(0.0),
815           time_generate_surface(0.0),
816           time_generate_bump(0.0),
817           time_generate_volume(0.0),
818           time_generate_displacement(0.0),
819           time_total(0.0)
820 {
821 }
822
823 string SVMCompiler::Summary::full_report() const
824 {
825         string report = "";
826         report += string_printf("Number of SVM nodes: %d\n", num_svm_nodes);
827         report += string_printf("Peak stack usage:    %d\n", peak_stack_usage);
828
829         report += string_printf("Time (in seconds):\n");
830         report += string_printf("  Finalize:          %f\n", time_finalize);
831         report += string_printf("  Bump finalize:     %f\n", time_finalize_bump);
832         report += string_printf("Finalize:            %f\n", time_finalize +
833                                                              time_finalize_bump);
834         report += string_printf("  Surface:           %f\n", time_generate_surface);
835         report += string_printf("  Bump:              %f\n", time_generate_bump);
836         report += string_printf("  Volume:            %f\n", time_generate_volume);
837         report += string_printf("  Displacement:      %f\n", time_generate_displacement);
838         report += string_printf("Generate:            %f\n", time_generate_surface +
839                                                              time_generate_bump +
840                                                              time_generate_volume +
841                                                              time_generate_displacement);
842         report += string_printf("Total:               %f\n", time_total);
843
844         return report;
845 }
846
847 CCL_NAMESPACE_END
848