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