bsdf_ashikhmin_velvet.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * 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 __BSDF_ASHIKHMIN_VELVET_H__
  33. #define __BSDF_ASHIKHMIN_VELVET_H__
  34. CCL_NAMESPACE_BEGIN
  35. typedef ccl_addr_space struct VelvetBsdf {
  36. SHADER_CLOSURE_BASE;
  37. float sigma;
  38. float invsigma2;
  39. } VelvetBsdf;
  40. static_assert(sizeof(ShaderClosure) >= sizeof(VelvetBsdf), "VelvetBsdf is too large!");
  41. ccl_device int bsdf_ashikhmin_velvet_setup(VelvetBsdf *bsdf)
  42. {
  43. float sigma = fmaxf(bsdf->sigma, 0.01f);
  44. bsdf->invsigma2 = 1.0f / (sigma * sigma);
  45. bsdf->type = CLOSURE_BSDF_ASHIKHMIN_VELVET_ID;
  46. return SD_BSDF | SD_BSDF_HAS_EVAL;
  47. }
  48. ccl_device bool bsdf_ashikhmin_velvet_merge(const ShaderClosure *a, const ShaderClosure *b)
  49. {
  50. const VelvetBsdf *bsdf_a = (const VelvetBsdf *)a;
  51. const VelvetBsdf *bsdf_b = (const VelvetBsdf *)b;
  52. return (isequal_float3(bsdf_a->N, bsdf_b->N)) && (bsdf_a->sigma == bsdf_b->sigma);
  53. }
  54. ccl_device float3 bsdf_ashikhmin_velvet_eval_reflect(const ShaderClosure *sc,
  55. const float3 I,
  56. const float3 omega_in,
  57. float *pdf)
  58. {
  59. const VelvetBsdf *bsdf = (const VelvetBsdf *)sc;
  60. float m_invsigma2 = bsdf->invsigma2;
  61. float3 N = bsdf->N;
  62. float cosNO = dot(N, I);
  63. float cosNI = dot(N, omega_in);
  64. if (cosNO > 0 && cosNI > 0) {
  65. float3 H = normalize(omega_in + I);
  66. float cosNH = dot(N, H);
  67. float cosHO = fabsf(dot(I, H));
  68. if (!(fabsf(cosNH) < 1.0f - 1e-5f && cosHO > 1e-5f))
  69. return make_float3(0.0f, 0.0f, 0.0f);
  70. float cosNHdivHO = cosNH / cosHO;
  71. cosNHdivHO = fmaxf(cosNHdivHO, 1e-5f);
  72. float fac1 = 2 * fabsf(cosNHdivHO * cosNO);
  73. float fac2 = 2 * fabsf(cosNHdivHO * cosNI);
  74. float sinNH2 = 1 - cosNH * cosNH;
  75. float sinNH4 = sinNH2 * sinNH2;
  76. float cotangent2 = (cosNH * cosNH) / sinNH2;
  77. float D = expf(-cotangent2 * m_invsigma2) * m_invsigma2 * M_1_PI_F / sinNH4;
  78. float G = min(1.0f, min(fac1, fac2)); // TODO: derive G from D analytically
  79. float out = 0.25f * (D * G) / cosNO;
  80. *pdf = 0.5f * M_1_PI_F;
  81. return make_float3(out, out, out);
  82. }
  83. return make_float3(0.0f, 0.0f, 0.0f);
  84. }
  85. ccl_device float3 bsdf_ashikhmin_velvet_eval_transmit(const ShaderClosure *sc,
  86. const float3 I,
  87. const float3 omega_in,
  88. float *pdf)
  89. {
  90. return make_float3(0.0f, 0.0f, 0.0f);
  91. }
  92. ccl_device int bsdf_ashikhmin_velvet_sample(const ShaderClosure *sc,
  93. float3 Ng,
  94. float3 I,
  95. float3 dIdx,
  96. float3 dIdy,
  97. float randu,
  98. float randv,
  99. float3 *eval,
  100. float3 *omega_in,
  101. float3 *domega_in_dx,
  102. float3 *domega_in_dy,
  103. float *pdf)
  104. {
  105. const VelvetBsdf *bsdf = (const VelvetBsdf *)sc;
  106. float m_invsigma2 = bsdf->invsigma2;
  107. float3 N = bsdf->N;
  108. // we are viewing the surface from above - send a ray out with uniform
  109. // distribution over the hemisphere
  110. sample_uniform_hemisphere(N, randu, randv, omega_in, pdf);
  111. if (dot(Ng, *omega_in) > 0) {
  112. float3 H = normalize(*omega_in + I);
  113. float cosNI = dot(N, *omega_in);
  114. float cosNO = dot(N, I);
  115. float cosNH = dot(N, H);
  116. float cosHO = fabsf(dot(I, H));
  117. if (fabsf(cosNO) > 1e-5f && fabsf(cosNH) < 1.0f - 1e-5f && cosHO > 1e-5f) {
  118. float cosNHdivHO = cosNH / cosHO;
  119. cosNHdivHO = fmaxf(cosNHdivHO, 1e-5f);
  120. float fac1 = 2 * fabsf(cosNHdivHO * cosNO);
  121. float fac2 = 2 * fabsf(cosNHdivHO * cosNI);
  122. float sinNH2 = 1 - cosNH * cosNH;
  123. float sinNH4 = sinNH2 * sinNH2;
  124. float cotangent2 = (cosNH * cosNH) / sinNH2;
  125. float D = expf(-cotangent2 * m_invsigma2) * m_invsigma2 * M_1_PI_F / sinNH4;
  126. float G = min(1.0f, min(fac1, fac2)); // TODO: derive G from D analytically
  127. float power = 0.25f * (D * G) / cosNO;
  128. *eval = make_float3(power, power, power);
  129. #ifdef __RAY_DIFFERENTIALS__
  130. // TODO: find a better approximation for the retroreflective bounce
  131. *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
  132. *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
  133. #endif
  134. }
  135. else
  136. *pdf = 0.0f;
  137. }
  138. else
  139. *pdf = 0.0f;
  140. return LABEL_REFLECT | LABEL_DIFFUSE;
  141. }
  142. CCL_NAMESPACE_END
  143. #endif /* __BSDF_ASHIKHMIN_VELVET_H__ */