bsdf_diffuse.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_DIFFUSE_H__
  33. #define __BSDF_DIFFUSE_H__
  34. CCL_NAMESPACE_BEGIN
  35. typedef ccl_addr_space struct DiffuseBsdf {
  36. SHADER_CLOSURE_BASE;
  37. } DiffuseBsdf;
  38. static_assert(sizeof(ShaderClosure) >= sizeof(DiffuseBsdf), "DiffuseBsdf is too large!");
  39. /* DIFFUSE */
  40. ccl_device int bsdf_diffuse_setup(DiffuseBsdf *bsdf)
  41. {
  42. bsdf->type = CLOSURE_BSDF_DIFFUSE_ID;
  43. return SD_BSDF | SD_BSDF_HAS_EVAL;
  44. }
  45. ccl_device bool bsdf_diffuse_merge(const ShaderClosure *a, const ShaderClosure *b)
  46. {
  47. const DiffuseBsdf *bsdf_a = (const DiffuseBsdf *)a;
  48. const DiffuseBsdf *bsdf_b = (const DiffuseBsdf *)b;
  49. return (isequal_float3(bsdf_a->N, bsdf_b->N));
  50. }
  51. ccl_device float3 bsdf_diffuse_eval_reflect(const ShaderClosure *sc,
  52. const float3 I,
  53. const float3 omega_in,
  54. float *pdf)
  55. {
  56. const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
  57. float3 N = bsdf->N;
  58. float cos_pi = fmaxf(dot(N, omega_in), 0.0f) * M_1_PI_F;
  59. *pdf = cos_pi;
  60. return make_float3(cos_pi, cos_pi, cos_pi);
  61. }
  62. ccl_device float3 bsdf_diffuse_eval_transmit(const ShaderClosure *sc,
  63. const float3 I,
  64. const float3 omega_in,
  65. float *pdf)
  66. {
  67. return make_float3(0.0f, 0.0f, 0.0f);
  68. }
  69. ccl_device int bsdf_diffuse_sample(const ShaderClosure *sc,
  70. float3 Ng,
  71. float3 I,
  72. float3 dIdx,
  73. float3 dIdy,
  74. float randu,
  75. float randv,
  76. float3 *eval,
  77. float3 *omega_in,
  78. float3 *domega_in_dx,
  79. float3 *domega_in_dy,
  80. float *pdf)
  81. {
  82. const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
  83. float3 N = bsdf->N;
  84. // distribution over the hemisphere
  85. sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
  86. if (dot(Ng, *omega_in) > 0.0f) {
  87. *eval = make_float3(*pdf, *pdf, *pdf);
  88. #ifdef __RAY_DIFFERENTIALS__
  89. // TODO: find a better approximation for the diffuse bounce
  90. *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
  91. *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
  92. #endif
  93. }
  94. else
  95. *pdf = 0.0f;
  96. return LABEL_REFLECT | LABEL_DIFFUSE;
  97. }
  98. /* TRANSLUCENT */
  99. ccl_device int bsdf_translucent_setup(DiffuseBsdf *bsdf)
  100. {
  101. bsdf->type = CLOSURE_BSDF_TRANSLUCENT_ID;
  102. return SD_BSDF | SD_BSDF_HAS_EVAL;
  103. }
  104. ccl_device float3 bsdf_translucent_eval_reflect(const ShaderClosure *sc,
  105. const float3 I,
  106. const float3 omega_in,
  107. float *pdf)
  108. {
  109. return make_float3(0.0f, 0.0f, 0.0f);
  110. }
  111. ccl_device float3 bsdf_translucent_eval_transmit(const ShaderClosure *sc,
  112. const float3 I,
  113. const float3 omega_in,
  114. float *pdf)
  115. {
  116. const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
  117. float3 N = bsdf->N;
  118. float cos_pi = fmaxf(-dot(N, omega_in), 0.0f) * M_1_PI_F;
  119. *pdf = cos_pi;
  120. return make_float3(cos_pi, cos_pi, cos_pi);
  121. }
  122. ccl_device int bsdf_translucent_sample(const ShaderClosure *sc,
  123. float3 Ng,
  124. float3 I,
  125. float3 dIdx,
  126. float3 dIdy,
  127. float randu,
  128. float randv,
  129. float3 *eval,
  130. float3 *omega_in,
  131. float3 *domega_in_dx,
  132. float3 *domega_in_dy,
  133. float *pdf)
  134. {
  135. const DiffuseBsdf *bsdf = (const DiffuseBsdf *)sc;
  136. float3 N = bsdf->N;
  137. // we are viewing the surface from the right side - send a ray out with cosine
  138. // distribution over the hemisphere
  139. sample_cos_hemisphere(-N, randu, randv, omega_in, pdf);
  140. if (dot(Ng, *omega_in) < 0) {
  141. *eval = make_float3(*pdf, *pdf, *pdf);
  142. #ifdef __RAY_DIFFERENTIALS__
  143. // TODO: find a better approximation for the diffuse bounce
  144. *domega_in_dx = -((2 * dot(N, dIdx)) * N - dIdx);
  145. *domega_in_dy = -((2 * dot(N, dIdy)) * N - dIdy);
  146. #endif
  147. }
  148. else {
  149. *pdf = 0;
  150. }
  151. return LABEL_TRANSMIT | LABEL_DIFFUSE;
  152. }
  153. CCL_NAMESPACE_END
  154. #endif /* __BSDF_DIFFUSE_H__ */