integrator.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "device/device.h"
  17. #include "render/background.h"
  18. #include "render/integrator.h"
  19. #include "render/film.h"
  20. #include "render/light.h"
  21. #include "render/scene.h"
  22. #include "render/shader.h"
  23. #include "render/sobol.h"
  24. #include "util/util_foreach.h"
  25. #include "util/util_hash.h"
  26. CCL_NAMESPACE_BEGIN
  27. NODE_DEFINE(Integrator)
  28. {
  29. NodeType *type = NodeType::add("integrator", create);
  30. SOCKET_INT(min_bounce, "Min Bounce", 0);
  31. SOCKET_INT(max_bounce, "Max Bounce", 7);
  32. SOCKET_INT(max_diffuse_bounce, "Max Diffuse Bounce", 7);
  33. SOCKET_INT(max_glossy_bounce, "Max Glossy Bounce", 7);
  34. SOCKET_INT(max_transmission_bounce, "Max Transmission Bounce", 7);
  35. SOCKET_INT(max_volume_bounce, "Max Volume Bounce", 7);
  36. SOCKET_INT(transparent_min_bounce, "Transparent Min Bounce", 0);
  37. SOCKET_INT(transparent_max_bounce, "Transparent Max Bounce", 7);
  38. SOCKET_INT(ao_bounces, "AO Bounces", 0);
  39. SOCKET_INT(volume_max_steps, "Volume Max Steps", 1024);
  40. SOCKET_FLOAT(volume_step_size, "Volume Step Size", 0.1f);
  41. SOCKET_BOOLEAN(caustics_reflective, "Reflective Caustics", true);
  42. SOCKET_BOOLEAN(caustics_refractive, "Refractive Caustics", true);
  43. SOCKET_FLOAT(filter_glossy, "Filter Glossy", 0.0f);
  44. SOCKET_INT(seed, "Seed", 0);
  45. SOCKET_FLOAT(sample_clamp_direct, "Sample Clamp Direct", 0.0f);
  46. SOCKET_FLOAT(sample_clamp_indirect, "Sample Clamp Indirect", 0.0f);
  47. SOCKET_BOOLEAN(motion_blur, "Motion Blur", false);
  48. SOCKET_INT(aa_samples, "AA Samples", 0);
  49. SOCKET_INT(diffuse_samples, "Diffuse Samples", 1);
  50. SOCKET_INT(glossy_samples, "Glossy Samples", 1);
  51. SOCKET_INT(transmission_samples, "Transmission Samples", 1);
  52. SOCKET_INT(ao_samples, "AO Samples", 1);
  53. SOCKET_INT(mesh_light_samples, "Mesh Light Samples", 1);
  54. SOCKET_INT(subsurface_samples, "Subsurface Samples", 1);
  55. SOCKET_INT(volume_samples, "Volume Samples", 1);
  56. SOCKET_INT(start_sample, "Start Sample", 0);
  57. SOCKET_BOOLEAN(sample_all_lights_direct, "Sample All Lights Direct", true);
  58. SOCKET_BOOLEAN(sample_all_lights_indirect, "Sample All Lights Indirect", true);
  59. SOCKET_FLOAT(light_sampling_threshold, "Light Sampling Threshold", 0.05f);
  60. static NodeEnum method_enum;
  61. method_enum.insert("path", PATH);
  62. method_enum.insert("branched_path", BRANCHED_PATH);
  63. SOCKET_ENUM(method, "Method", method_enum, PATH);
  64. static NodeEnum sampling_pattern_enum;
  65. sampling_pattern_enum.insert("sobol", SAMPLING_PATTERN_SOBOL);
  66. sampling_pattern_enum.insert("cmj", SAMPLING_PATTERN_CMJ);
  67. SOCKET_ENUM(sampling_pattern, "Sampling Pattern", sampling_pattern_enum, SAMPLING_PATTERN_SOBOL);
  68. return type;
  69. }
  70. Integrator::Integrator() : Node(node_type)
  71. {
  72. need_update = true;
  73. }
  74. Integrator::~Integrator()
  75. {
  76. }
  77. void Integrator::device_update(Device *device, DeviceScene *dscene, Scene *scene)
  78. {
  79. if (!need_update)
  80. return;
  81. device_free(device, dscene);
  82. KernelIntegrator *kintegrator = &dscene->data.integrator;
  83. /* integrator parameters */
  84. kintegrator->min_bounce = min_bounce + 1;
  85. kintegrator->max_bounce = max_bounce + 1;
  86. kintegrator->max_diffuse_bounce = max_diffuse_bounce + 1;
  87. kintegrator->max_glossy_bounce = max_glossy_bounce + 1;
  88. kintegrator->max_transmission_bounce = max_transmission_bounce + 1;
  89. kintegrator->max_volume_bounce = max_volume_bounce + 1;
  90. kintegrator->transparent_min_bounce = transparent_min_bounce + 1;
  91. kintegrator->transparent_max_bounce = transparent_max_bounce + 1;
  92. if (ao_bounces == 0) {
  93. kintegrator->ao_bounces = INT_MAX;
  94. }
  95. else {
  96. kintegrator->ao_bounces = ao_bounces - 1;
  97. }
  98. /* Transparent Shadows
  99. * We only need to enable transparent shadows, if we actually have
  100. * transparent shaders in the scene. Otherwise we can disable it
  101. * to improve performance a bit. */
  102. kintegrator->transparent_shadows = false;
  103. foreach (Shader *shader, scene->shaders) {
  104. /* keep this in sync with SD_HAS_TRANSPARENT_SHADOW in shader.cpp */
  105. if ((shader->has_surface_transparent && shader->use_transparent_shadow) ||
  106. shader->has_volume) {
  107. kintegrator->transparent_shadows = true;
  108. break;
  109. }
  110. }
  111. kintegrator->volume_max_steps = volume_max_steps;
  112. kintegrator->volume_step_size = volume_step_size;
  113. kintegrator->caustics_reflective = caustics_reflective;
  114. kintegrator->caustics_refractive = caustics_refractive;
  115. kintegrator->filter_glossy = (filter_glossy == 0.0f) ? FLT_MAX : 1.0f / filter_glossy;
  116. kintegrator->seed = hash_int(seed);
  117. kintegrator->use_ambient_occlusion = ((Pass::contains(scene->film->passes, PASS_AO)) ||
  118. dscene->data.background.ao_factor != 0.0f);
  119. kintegrator->sample_clamp_direct = (sample_clamp_direct == 0.0f) ? FLT_MAX :
  120. sample_clamp_direct * 3.0f;
  121. kintegrator->sample_clamp_indirect = (sample_clamp_indirect == 0.0f) ?
  122. FLT_MAX :
  123. sample_clamp_indirect * 3.0f;
  124. kintegrator->branched = (method == BRANCHED_PATH);
  125. kintegrator->volume_decoupled = device->info.has_volume_decoupled;
  126. kintegrator->diffuse_samples = diffuse_samples;
  127. kintegrator->glossy_samples = glossy_samples;
  128. kintegrator->transmission_samples = transmission_samples;
  129. kintegrator->ao_samples = ao_samples;
  130. kintegrator->mesh_light_samples = mesh_light_samples;
  131. kintegrator->subsurface_samples = subsurface_samples;
  132. kintegrator->volume_samples = volume_samples;
  133. kintegrator->start_sample = start_sample;
  134. if (method == BRANCHED_PATH) {
  135. kintegrator->sample_all_lights_direct = sample_all_lights_direct;
  136. kintegrator->sample_all_lights_indirect = sample_all_lights_indirect;
  137. }
  138. else {
  139. kintegrator->sample_all_lights_direct = false;
  140. kintegrator->sample_all_lights_indirect = false;
  141. }
  142. kintegrator->sampling_pattern = sampling_pattern;
  143. kintegrator->aa_samples = aa_samples;
  144. if (light_sampling_threshold > 0.0f) {
  145. kintegrator->light_inv_rr_threshold = 1.0f / light_sampling_threshold;
  146. }
  147. else {
  148. kintegrator->light_inv_rr_threshold = 0.0f;
  149. }
  150. /* sobol directions table */
  151. int max_samples = 1;
  152. if (method == BRANCHED_PATH) {
  153. foreach (Light *light, scene->lights)
  154. max_samples = max(max_samples, light->samples);
  155. max_samples = max(max_samples,
  156. max(diffuse_samples, max(glossy_samples, transmission_samples)));
  157. max_samples = max(max_samples, max(ao_samples, max(mesh_light_samples, subsurface_samples)));
  158. max_samples = max(max_samples, volume_samples);
  159. }
  160. uint total_bounces = max_bounce + transparent_max_bounce + 3 + VOLUME_BOUNDS_MAX +
  161. max(BSSRDF_MAX_HITS, BSSRDF_MAX_BOUNCES);
  162. max_samples *= total_bounces;
  163. int dimensions = PRNG_BASE_NUM + max_samples * PRNG_BOUNCE_NUM;
  164. dimensions = min(dimensions, SOBOL_MAX_DIMENSIONS);
  165. uint *directions = dscene->sobol_directions.alloc(SOBOL_BITS * dimensions);
  166. sobol_generate_direction_vectors((uint(*)[SOBOL_BITS])directions, dimensions);
  167. dscene->sobol_directions.copy_to_device();
  168. /* Clamping. */
  169. bool use_sample_clamp = (sample_clamp_direct != 0.0f || sample_clamp_indirect != 0.0f);
  170. if (use_sample_clamp != scene->film->use_sample_clamp) {
  171. scene->film->use_sample_clamp = use_sample_clamp;
  172. scene->film->tag_update(scene);
  173. }
  174. need_update = false;
  175. }
  176. void Integrator::device_free(Device *, DeviceScene *dscene)
  177. {
  178. dscene->sobol_directions.free();
  179. }
  180. bool Integrator::modified(const Integrator &integrator)
  181. {
  182. return !Node::equals(integrator);
  183. }
  184. void Integrator::tag_update(Scene *scene)
  185. {
  186. foreach (Shader *shader, scene->shaders) {
  187. if (shader->has_integrator_dependency) {
  188. scene->shader_manager->need_update = true;
  189. break;
  190. }
  191. }
  192. need_update = true;
  193. }
  194. CCL_NAMESPACE_END