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