kernel_montecarlo.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Parts adapted from Open Shading Language with this license:
  3. *
  4. * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
  5. * All Rights Reserved.
  6. *
  7. * Modifications Copyright 2011, Blender Foundation.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * * Neither the name of Sony Pictures Imageworks nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifndef __KERNEL_MONTECARLO_CL__
  33. #define __KERNEL_MONTECARLO_CL__
  34. CCL_NAMESPACE_BEGIN
  35. /* distribute uniform xy on [0,1] over unit disk [-1,1] */
  36. ccl_device void to_unit_disk(float *x, float *y)
  37. {
  38. float phi = M_2PI_F * (*x);
  39. float r = sqrtf(*y);
  40. *x = r * cosf(phi);
  41. *y = r * sinf(phi);
  42. }
  43. /* return an orthogonal tangent and bitangent given a normal and tangent that
  44. * may not be exactly orthogonal */
  45. ccl_device void make_orthonormals_tangent(const float3 N, const float3 T, float3 *a, float3 *b)
  46. {
  47. *b = normalize(cross(N, T));
  48. *a = cross(*b, N);
  49. }
  50. /* sample direction with cosine weighted distributed in hemisphere */
  51. ccl_device_inline void sample_cos_hemisphere(
  52. const float3 N, float randu, float randv, float3 *omega_in, float *pdf)
  53. {
  54. to_unit_disk(&randu, &randv);
  55. float costheta = sqrtf(max(1.0f - randu * randu - randv * randv, 0.0f));
  56. float3 T, B;
  57. make_orthonormals(N, &T, &B);
  58. *omega_in = randu * T + randv * B + costheta * N;
  59. *pdf = costheta * M_1_PI_F;
  60. }
  61. /* sample direction uniformly distributed in hemisphere */
  62. ccl_device_inline void sample_uniform_hemisphere(
  63. const float3 N, float randu, float randv, float3 *omega_in, float *pdf)
  64. {
  65. float z = randu;
  66. float r = sqrtf(max(0.0f, 1.0f - z * z));
  67. float phi = M_2PI_F * randv;
  68. float x = r * cosf(phi);
  69. float y = r * sinf(phi);
  70. float3 T, B;
  71. make_orthonormals(N, &T, &B);
  72. *omega_in = x * T + y * B + z * N;
  73. *pdf = 0.5f * M_1_PI_F;
  74. }
  75. /* sample direction uniformly distributed in cone */
  76. ccl_device_inline void sample_uniform_cone(
  77. const float3 N, float angle, float randu, float randv, float3 *omega_in, float *pdf)
  78. {
  79. float z = cosf(angle * randu);
  80. float r = sqrtf(max(0.0f, 1.0f - z * z));
  81. float phi = M_2PI_F * randv;
  82. float x = r * cosf(phi);
  83. float y = r * sinf(phi);
  84. float3 T, B;
  85. make_orthonormals(N, &T, &B);
  86. *omega_in = x * T + y * B + z * N;
  87. *pdf = 0.5f * M_1_PI_F / (1.0f - cosf(angle));
  88. }
  89. /* sample uniform point on the surface of a sphere */
  90. ccl_device float3 sample_uniform_sphere(float u1, float u2)
  91. {
  92. float z = 1.0f - 2.0f * u1;
  93. float r = sqrtf(fmaxf(0.0f, 1.0f - z * z));
  94. float phi = M_2PI_F * u2;
  95. float x = r * cosf(phi);
  96. float y = r * sinf(phi);
  97. return make_float3(x, y, z);
  98. }
  99. ccl_device float balance_heuristic(float a, float b)
  100. {
  101. return (a) / (a + b);
  102. }
  103. ccl_device float balance_heuristic_3(float a, float b, float c)
  104. {
  105. return (a) / (a + b + c);
  106. }
  107. ccl_device float power_heuristic(float a, float b)
  108. {
  109. return (a * a) / (a * a + b * b);
  110. }
  111. ccl_device float power_heuristic_3(float a, float b, float c)
  112. {
  113. return (a * a) / (a * a + b * b + c * c);
  114. }
  115. ccl_device float max_heuristic(float a, float b)
  116. {
  117. return (a > b) ? 1.0f : 0.0f;
  118. }
  119. /* distribute uniform xy on [0,1] over unit disk [-1,1], with concentric mapping
  120. * to better preserve stratification for some RNG sequences */
  121. ccl_device float2 concentric_sample_disk(float u1, float u2)
  122. {
  123. float phi, r;
  124. float a = 2.0f * u1 - 1.0f;
  125. float b = 2.0f * u2 - 1.0f;
  126. if (a == 0.0f && b == 0.0f) {
  127. return make_float2(0.0f, 0.0f);
  128. }
  129. else if (a * a > b * b) {
  130. r = a;
  131. phi = M_PI_4_F * (b / a);
  132. }
  133. else {
  134. r = b;
  135. phi = M_PI_2_F - M_PI_4_F * (a / b);
  136. }
  137. return make_float2(r * cosf(phi), r * sinf(phi));
  138. }
  139. /* sample point in unit polygon with given number of corners and rotation */
  140. ccl_device float2 regular_polygon_sample(float corners, float rotation, float u, float v)
  141. {
  142. /* sample corner number and reuse u */
  143. float corner = floorf(u * corners);
  144. u = u * corners - corner;
  145. /* uniform sampled triangle weights */
  146. u = sqrtf(u);
  147. v = v * u;
  148. u = 1.0f - u;
  149. /* point in triangle */
  150. float angle = M_PI_F / corners;
  151. float2 p = make_float2((u + v) * cosf(angle), (u - v) * sinf(angle));
  152. /* rotate */
  153. rotation += corner * 2.0f * angle;
  154. float cr = cosf(rotation);
  155. float sr = sinf(rotation);
  156. return make_float2(cr * p.x - sr * p.y, sr * p.x + cr * p.y);
  157. }
  158. ccl_device float3 ensure_valid_reflection(float3 Ng, float3 I, float3 N)
  159. {
  160. float3 R = 2 * dot(N, I) * N - I;
  161. /* Reflection rays may always be at least as shallow as the incoming ray. */
  162. float threshold = min(0.9f * dot(Ng, I), 0.01f);
  163. if (dot(Ng, R) >= threshold) {
  164. return N;
  165. }
  166. /* Form coordinate system with Ng as the Z axis and N inside the X-Z-plane.
  167. * The X axis is found by normalizing the component of N that's orthogonal to Ng.
  168. * The Y axis isn't actually needed.
  169. */
  170. float NdotNg = dot(N, Ng);
  171. float3 X = normalize(N - NdotNg * Ng);
  172. /* Keep math expressions. */
  173. /* clang-format off */
  174. /* Calculate N.z and N.x in the local coordinate system.
  175. *
  176. * The goal of this computation is to find a N' that is rotated towards Ng just enough
  177. * to lift R' above the threshold (here called t), therefore dot(R', Ng) = t.
  178. *
  179. * According to the standard reflection equation,
  180. * this means that we want dot(2*dot(N', I)*N' - I, Ng) = t.
  181. *
  182. * Since the Z axis of our local coordinate system is Ng, dot(x, Ng) is just x.z, so we get
  183. * 2*dot(N', I)*N'.z - I.z = t.
  184. *
  185. * The rotation is simple to express in the coordinate system we formed -
  186. * since N lies in the X-Z-plane, we know that N' will also lie in the X-Z-plane,
  187. * so N'.y = 0 and therefore dot(N', I) = N'.x*I.x + N'.z*I.z .
  188. *
  189. * Furthermore, we want N' to be normalized, so N'.x = sqrt(1 - N'.z^2).
  190. *
  191. * With these simplifications,
  192. * we get the final equation 2*(sqrt(1 - N'.z^2)*I.x + N'.z*I.z)*N'.z - I.z = t.
  193. *
  194. * The only unknown here is N'.z, so we can solve for that.
  195. *
  196. * The equation has four solutions in general:
  197. *
  198. * N'.z = +-sqrt(0.5*(+-sqrt(I.x^2*(I.x^2 + I.z^2 - t^2)) + t*I.z + I.x^2 + I.z^2)/(I.x^2 + I.z^2))
  199. * We can simplify this expression a bit by grouping terms:
  200. *
  201. * a = I.x^2 + I.z^2
  202. * b = sqrt(I.x^2 * (a - t^2))
  203. * c = I.z*t + a
  204. * N'.z = +-sqrt(0.5*(+-b + c)/a)
  205. *
  206. * Two solutions can immediately be discarded because they're negative so N' would lie in the
  207. * lower hemisphere.
  208. */
  209. /* clang-format on */
  210. float Ix = dot(I, X), Iz = dot(I, Ng);
  211. float Ix2 = sqr(Ix), Iz2 = sqr(Iz);
  212. float a = Ix2 + Iz2;
  213. float b = safe_sqrtf(Ix2 * (a - sqr(threshold)));
  214. float c = Iz * threshold + a;
  215. /* Evaluate both solutions.
  216. * In many cases one can be immediately discarded (if N'.z would be imaginary or larger than
  217. * one), so check for that first. If no option is viable (might happen in extreme cases like N
  218. * being in the wrong hemisphere), give up and return Ng. */
  219. float fac = 0.5f / a;
  220. float N1_z2 = fac * (b + c), N2_z2 = fac * (-b + c);
  221. bool valid1 = (N1_z2 > 1e-5f) && (N1_z2 <= (1.0f + 1e-5f));
  222. bool valid2 = (N2_z2 > 1e-5f) && (N2_z2 <= (1.0f + 1e-5f));
  223. float2 N_new;
  224. if (valid1 && valid2) {
  225. /* If both are possible, do the expensive reflection-based check. */
  226. float2 N1 = make_float2(safe_sqrtf(1.0f - N1_z2), safe_sqrtf(N1_z2));
  227. float2 N2 = make_float2(safe_sqrtf(1.0f - N2_z2), safe_sqrtf(N2_z2));
  228. float R1 = 2 * (N1.x * Ix + N1.y * Iz) * N1.y - Iz;
  229. float R2 = 2 * (N2.x * Ix + N2.y * Iz) * N2.y - Iz;
  230. valid1 = (R1 >= 1e-5f);
  231. valid2 = (R2 >= 1e-5f);
  232. if (valid1 && valid2) {
  233. /* If both solutions are valid, return the one with the shallower reflection since it will be
  234. * closer to the input (if the original reflection wasn't shallow, we would not be in this
  235. * part of the function). */
  236. N_new = (R1 < R2) ? N1 : N2;
  237. }
  238. else {
  239. /* If only one reflection is valid (= positive), pick that one. */
  240. N_new = (R1 > R2) ? N1 : N2;
  241. }
  242. }
  243. else if (valid1 || valid2) {
  244. /* Only one solution passes the N'.z criterium, so pick that one. */
  245. float Nz2 = valid1 ? N1_z2 : N2_z2;
  246. N_new = make_float2(safe_sqrtf(1.0f - Nz2), safe_sqrtf(Nz2));
  247. }
  248. else {
  249. return Ng;
  250. }
  251. return N_new.x * X + N_new.y * Ng;
  252. }
  253. CCL_NAMESPACE_END
  254. #endif /* __KERNEL_MONTECARLO_CL__ */