volume.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #ifndef __VOLUME_H__
  17. #define __VOLUME_H__
  18. CCL_NAMESPACE_BEGIN
  19. /* VOLUME EXTINCTION */
  20. ccl_device void volume_extinction_setup(ShaderData *sd, float3 weight)
  21. {
  22. if (sd->flag & SD_EXTINCTION) {
  23. sd->closure_transparent_extinction += weight;
  24. }
  25. else {
  26. sd->flag |= SD_EXTINCTION;
  27. sd->closure_transparent_extinction = weight;
  28. }
  29. }
  30. /* HENYEY-GREENSTEIN CLOSURE */
  31. typedef ccl_addr_space struct HenyeyGreensteinVolume {
  32. SHADER_CLOSURE_BASE;
  33. float g;
  34. } HenyeyGreensteinVolume;
  35. static_assert(sizeof(ShaderClosure) >= sizeof(HenyeyGreensteinVolume),
  36. "HenyeyGreensteinVolume is too large!");
  37. /* Given cosine between rays, return probability density that a photon bounces
  38. * to that direction. The g parameter controls how different it is from the
  39. * uniform sphere. g=0 uniform diffuse-like, g=1 close to sharp single ray. */
  40. ccl_device float single_peaked_henyey_greenstein(float cos_theta, float g)
  41. {
  42. return ((1.0f - g * g) / safe_powf(1.0f + g * g - 2.0f * g * cos_theta, 1.5f)) *
  43. (M_1_PI_F * 0.25f);
  44. };
  45. ccl_device int volume_henyey_greenstein_setup(HenyeyGreensteinVolume *volume)
  46. {
  47. volume->type = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
  48. /* clamp anisotropy to avoid delta function */
  49. volume->g = signf(volume->g) * min(fabsf(volume->g), 1.0f - 1e-3f);
  50. return SD_SCATTER;
  51. }
  52. ccl_device bool volume_henyey_greenstein_merge(const ShaderClosure *a, const ShaderClosure *b)
  53. {
  54. const HenyeyGreensteinVolume *volume_a = (const HenyeyGreensteinVolume *)a;
  55. const HenyeyGreensteinVolume *volume_b = (const HenyeyGreensteinVolume *)b;
  56. return (volume_a->g == volume_b->g);
  57. }
  58. ccl_device float3 volume_henyey_greenstein_eval_phase(const ShaderClosure *sc,
  59. const float3 I,
  60. float3 omega_in,
  61. float *pdf)
  62. {
  63. const HenyeyGreensteinVolume *volume = (const HenyeyGreensteinVolume *)sc;
  64. float g = volume->g;
  65. /* note that I points towards the viewer */
  66. if (fabsf(g) < 1e-3f) {
  67. *pdf = M_1_PI_F * 0.25f;
  68. }
  69. else {
  70. float cos_theta = dot(-I, omega_in);
  71. *pdf = single_peaked_henyey_greenstein(cos_theta, g);
  72. }
  73. return make_float3(*pdf, *pdf, *pdf);
  74. }
  75. ccl_device float3
  76. henyey_greenstrein_sample(float3 D, float g, float randu, float randv, float *pdf)
  77. {
  78. /* match pdf for small g */
  79. float cos_theta;
  80. bool isotropic = fabsf(g) < 1e-3f;
  81. if (isotropic) {
  82. cos_theta = (1.0f - 2.0f * randu);
  83. if (pdf) {
  84. *pdf = M_1_PI_F * 0.25f;
  85. }
  86. }
  87. else {
  88. float k = (1.0f - g * g) / (1.0f - g + 2.0f * g * randu);
  89. cos_theta = (1.0f + g * g - k * k) / (2.0f * g);
  90. if (pdf) {
  91. *pdf = single_peaked_henyey_greenstein(cos_theta, g);
  92. }
  93. }
  94. float sin_theta = safe_sqrtf(1.0f - cos_theta * cos_theta);
  95. float phi = M_2PI_F * randv;
  96. float3 dir = make_float3(sin_theta * cosf(phi), sin_theta * sinf(phi), cos_theta);
  97. float3 T, B;
  98. make_orthonormals(D, &T, &B);
  99. dir = dir.x * T + dir.y * B + dir.z * D;
  100. return dir;
  101. }
  102. ccl_device int volume_henyey_greenstein_sample(const ShaderClosure *sc,
  103. float3 I,
  104. float3 dIdx,
  105. float3 dIdy,
  106. float randu,
  107. float randv,
  108. float3 *eval,
  109. float3 *omega_in,
  110. float3 *domega_in_dx,
  111. float3 *domega_in_dy,
  112. float *pdf)
  113. {
  114. const HenyeyGreensteinVolume *volume = (const HenyeyGreensteinVolume *)sc;
  115. float g = volume->g;
  116. /* note that I points towards the viewer and so is used negated */
  117. *omega_in = henyey_greenstrein_sample(-I, g, randu, randv, pdf);
  118. *eval = make_float3(*pdf, *pdf, *pdf); /* perfect importance sampling */
  119. #ifdef __RAY_DIFFERENTIALS__
  120. /* todo: implement ray differential estimation */
  121. *domega_in_dx = make_float3(0.0f, 0.0f, 0.0f);
  122. *domega_in_dy = make_float3(0.0f, 0.0f, 0.0f);
  123. #endif
  124. return LABEL_VOLUME_SCATTER;
  125. }
  126. /* VOLUME CLOSURE */
  127. ccl_device float3 volume_phase_eval(const ShaderData *sd,
  128. const ShaderClosure *sc,
  129. float3 omega_in,
  130. float *pdf)
  131. {
  132. kernel_assert(sc->type == CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID);
  133. return volume_henyey_greenstein_eval_phase(sc, sd->I, omega_in, pdf);
  134. }
  135. ccl_device int volume_phase_sample(const ShaderData *sd,
  136. const ShaderClosure *sc,
  137. float randu,
  138. float randv,
  139. float3 *eval,
  140. float3 *omega_in,
  141. differential3 *domega_in,
  142. float *pdf)
  143. {
  144. int label;
  145. switch (sc->type) {
  146. case CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID:
  147. label = volume_henyey_greenstein_sample(sc,
  148. sd->I,
  149. sd->dI.dx,
  150. sd->dI.dy,
  151. randu,
  152. randv,
  153. eval,
  154. omega_in,
  155. &domega_in->dx,
  156. &domega_in->dy,
  157. pdf);
  158. break;
  159. default:
  160. *eval = make_float3(0.0f, 0.0f, 0.0f);
  161. label = LABEL_NONE;
  162. break;
  163. }
  164. return label;
  165. }
  166. CCL_NAMESPACE_END
  167. #endif