bsdf_diffuse_ramp.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 2012, 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_RAMP_H__
  33. #define __BSDF_DIFFUSE_RAMP_H__
  34. CCL_NAMESPACE_BEGIN
  35. #ifdef __OSL__
  36. typedef ccl_addr_space struct DiffuseRampBsdf {
  37. SHADER_CLOSURE_BASE;
  38. float3 *colors;
  39. } DiffuseRampBsdf;
  40. static_assert(sizeof(ShaderClosure) >= sizeof(DiffuseRampBsdf), "DiffuseRampBsdf is too large!");
  41. ccl_device float3 bsdf_diffuse_ramp_get_color(const float3 colors[8], float pos)
  42. {
  43. int MAXCOLORS = 8;
  44. float npos = pos * (float)(MAXCOLORS - 1);
  45. int ipos = float_to_int(npos);
  46. if (ipos < 0)
  47. return colors[0];
  48. if (ipos >= (MAXCOLORS - 1))
  49. return colors[MAXCOLORS - 1];
  50. float offset = npos - (float)ipos;
  51. return colors[ipos] * (1.0f - offset) + colors[ipos + 1] * offset;
  52. }
  53. ccl_device int bsdf_diffuse_ramp_setup(DiffuseRampBsdf *bsdf)
  54. {
  55. bsdf->type = CLOSURE_BSDF_DIFFUSE_RAMP_ID;
  56. return SD_BSDF | SD_BSDF_HAS_EVAL;
  57. }
  58. ccl_device void bsdf_diffuse_ramp_blur(ShaderClosure *sc, float roughness)
  59. {
  60. }
  61. ccl_device float3 bsdf_diffuse_ramp_eval_reflect(const ShaderClosure *sc,
  62. const float3 I,
  63. const float3 omega_in,
  64. float *pdf)
  65. {
  66. const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
  67. float3 N = bsdf->N;
  68. float cos_pi = fmaxf(dot(N, omega_in), 0.0f);
  69. *pdf = cos_pi * M_1_PI_F;
  70. return bsdf_diffuse_ramp_get_color(bsdf->colors, cos_pi) * M_1_PI_F;
  71. }
  72. ccl_device float3 bsdf_diffuse_ramp_eval_transmit(const ShaderClosure *sc,
  73. const float3 I,
  74. const float3 omega_in,
  75. float *pdf)
  76. {
  77. return make_float3(0.0f, 0.0f, 0.0f);
  78. }
  79. ccl_device int bsdf_diffuse_ramp_sample(const ShaderClosure *sc,
  80. float3 Ng,
  81. float3 I,
  82. float3 dIdx,
  83. float3 dIdy,
  84. float randu,
  85. float randv,
  86. float3 *eval,
  87. float3 *omega_in,
  88. float3 *domega_in_dx,
  89. float3 *domega_in_dy,
  90. float *pdf)
  91. {
  92. const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
  93. float3 N = bsdf->N;
  94. // distribution over the hemisphere
  95. sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
  96. if (dot(Ng, *omega_in) > 0.0f) {
  97. *eval = bsdf_diffuse_ramp_get_color(bsdf->colors, *pdf * M_PI_F) * M_1_PI_F;
  98. # ifdef __RAY_DIFFERENTIALS__
  99. *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
  100. *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
  101. # endif
  102. }
  103. else
  104. *pdf = 0.0f;
  105. return LABEL_REFLECT | LABEL_DIFFUSE;
  106. }
  107. #endif /* __OSL__ */
  108. CCL_NAMESPACE_END
  109. #endif /* __BSDF_DIFFUSE_RAMP_H__ */