background.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "render/background.h"
  17. #include "device/device.h"
  18. #include "render/integrator.h"
  19. #include "render/graph.h"
  20. #include "render/nodes.h"
  21. #include "render/scene.h"
  22. #include "render/shader.h"
  23. #include "util/util_foreach.h"
  24. #include "util/util_math.h"
  25. #include "util/util_types.h"
  26. CCL_NAMESPACE_BEGIN
  27. NODE_DEFINE(Background)
  28. {
  29. NodeType *type = NodeType::add("background", create);
  30. SOCKET_FLOAT(ao_factor, "AO Factor", 0.0f);
  31. SOCKET_FLOAT(ao_distance, "AO Distance", FLT_MAX);
  32. SOCKET_BOOLEAN(use_shader, "Use Shader", true);
  33. SOCKET_BOOLEAN(use_ao, "Use AO", false);
  34. SOCKET_UINT(visibility, "Visibility", PATH_RAY_ALL_VISIBILITY);
  35. SOCKET_BOOLEAN(transparent, "Transparent", false);
  36. SOCKET_BOOLEAN(transparent_glass, "Transparent Glass", false);
  37. SOCKET_FLOAT(transparent_roughness_threshold, "Transparent Roughness Threshold", 0.0f);
  38. SOCKET_NODE(shader, "Shader", &Shader::node_type);
  39. return type;
  40. }
  41. Background::Background() : Node(node_type)
  42. {
  43. need_update = true;
  44. }
  45. Background::~Background()
  46. {
  47. }
  48. void Background::device_update(Device *device, DeviceScene *dscene, Scene *scene)
  49. {
  50. if (!need_update)
  51. return;
  52. device_free(device, dscene);
  53. Shader *bg_shader = shader;
  54. if (use_shader) {
  55. if (!bg_shader)
  56. bg_shader = scene->default_background;
  57. }
  58. else
  59. bg_shader = scene->default_empty;
  60. /* set shader index and transparent option */
  61. KernelBackground *kbackground = &dscene->data.background;
  62. kbackground->ao_factor = (use_ao) ? ao_factor : 0.0f;
  63. kbackground->ao_bounces_factor = ao_factor;
  64. kbackground->ao_distance = ao_distance;
  65. kbackground->transparent = transparent;
  66. kbackground->surface_shader = scene->shader_manager->get_shader_id(bg_shader);
  67. if (transparent && transparent_glass) {
  68. /* Square twice, once for principled BSDF convention, and once for
  69. * faster comparison in kernel with anisotropic roughness. */
  70. kbackground->transparent_roughness_squared_threshold = sqr(
  71. sqr(transparent_roughness_threshold));
  72. }
  73. else {
  74. kbackground->transparent_roughness_squared_threshold = -1.0f;
  75. }
  76. if (bg_shader->has_volume)
  77. kbackground->volume_shader = kbackground->surface_shader;
  78. else
  79. kbackground->volume_shader = SHADER_NONE;
  80. /* No background node, make world shader invisible to all rays, to skip evaluation in kernel. */
  81. if (bg_shader->graph->nodes.size() <= 1) {
  82. kbackground->surface_shader |= SHADER_EXCLUDE_ANY;
  83. }
  84. /* Background present, check visibilities */
  85. else {
  86. if (!(visibility & PATH_RAY_DIFFUSE))
  87. kbackground->surface_shader |= SHADER_EXCLUDE_DIFFUSE;
  88. if (!(visibility & PATH_RAY_GLOSSY))
  89. kbackground->surface_shader |= SHADER_EXCLUDE_GLOSSY;
  90. if (!(visibility & PATH_RAY_TRANSMIT))
  91. kbackground->surface_shader |= SHADER_EXCLUDE_TRANSMIT;
  92. if (!(visibility & PATH_RAY_VOLUME_SCATTER))
  93. kbackground->surface_shader |= SHADER_EXCLUDE_SCATTER;
  94. if (!(visibility & PATH_RAY_CAMERA))
  95. kbackground->surface_shader |= SHADER_EXCLUDE_CAMERA;
  96. }
  97. need_update = false;
  98. }
  99. void Background::device_free(Device * /*device*/, DeviceScene * /*dscene*/)
  100. {
  101. }
  102. bool Background::modified(const Background &background)
  103. {
  104. return !Node::equals(background);
  105. }
  106. void Background::tag_update(Scene *scene)
  107. {
  108. scene->integrator->tag_update(scene);
  109. need_update = true;
  110. }
  111. CCL_NAMESPACE_END